文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>1130.多岛国

1130.多岛国

时间:2010-11-12  来源:gzzcracker

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int p[50001];
int r[50001];

int find(int u) {
    int y = u;
    int z = u;
    int w;
    while (p[y] != 0)
        y = p[y];
    while (p[z] != 0) {
        w = p[z];
        p[z] = y;
        z = w;
    }
    return y;
}

void unio(int u, int v) {
    if (r[u] >= r[v]) {
        p[v] = u;
        if (r[u] == r[v])
            r[u]++;
        return;
    }
    p[u] = v;
}

int main(int argc, char* argv[]) {
    int n, m, q;
    int u, v;
    char ch;

    scanf("%d %d", &n, &m);
    while (m--) {
        scanf("%d %d", &u, &v);
        u = find(u);
        v = find(v);
        if (u != v)
            unio(u, v);
    }

    scanf("%d", &q);
    while (q--) {
        scanf(" %c %d %d", &ch, &u, &v);
        u = find(u);
        v = find(v);
        if (ch == 'Q')
            printf("%s\n", u == v ? "yes" : "no");
        else if (u != v)
            unio(u, v);
    }

    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载