hdu1272 简单并查集

题目链接在这里

题目大意:

中文题目,自己看吧。

思路分析:

裸的并查集。。。太裸了。只需要注意只输入0 0的时候输出Yes

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < n; ++i)
#define clr(x) memset(x, 0, sizeof(x))
using namespace std;

const int MaxN = 1e5 + 10;
int par[MaxN], r[MaxN];
bool vis[MaxN];
int n, cnt, num_edge;
bool flag;
void init(){
    rep(i, MaxN)    par[i] = i;
    clr(r);
    clr(vis);
    n = cnt = num_edge = 0;
    flag = true;
}

int Find(int x){
    if(x == par[x]) return x;
    return par[x] = Find(par[x]);
}

bool unite(int x, int y){
    x = Find(x);
    y = Find(y);
    if(x == y)  return false;
    if(r[x] < r[y]) par[x] = y;
    else{
        par[y] = x;
        if(r[x] == r[y])    ++r[x];
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    int x, y;
    init();

    while(scanf("%d %d", &x, &y)){
        if(x == -1 && y == -1)  break;
        if(x == 0 && y == 0){
            if(cnt == 0){
                puts("Yes");
            }
            else{
                if(flag && num_edge == cnt - 1) puts("Yes");
                else puts("No");
            }

            init();
            continue;
        }
        ++num_edge;
        if(!vis[x]){
            ++cnt;
            vis[x] = true;
        }
        if(!vis[y]){
            ++cnt;
            vis[y] = true;
        }
        if(!unite(x, y))
            flag = false;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Never__Give_Up_/article/details/86372708
今日推荐