UVALive - 3644 X-Plosives 并查集

并查集果题

传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12648

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int bin[100005];
int cnt;

int findx(int x) {
    if(bin[x] != x) {
        bin[x] = findx(bin[x]);
    }
    return bin[x];
}

void merge(int x,int y){
    int fx = findx(x);
    int fy = findx(y);
    if(fx != fy) {
        bin[fx] = fy;
    }
    else cnt ++;
}

void Deal_with() {
    int a1,a2;
    while(~scanf("%d",&a1)) {
        for(int i = 0 ; i <= 100000 ; i++) {
            bin[i] = i;
        }
        cnt = 0;
        while(a1+1) {
            scanf("%d",&a2);
            merge(a1,a2);
            scanf("%d",&a1);
        }
        printf("%d\n",cnt);
    }
}

int main(void) {
    //freopen("a.in","r",stdin);
    Deal_with();
    return 0;
}



你可能感兴趣的:(UVALive - 3644 X-Plosives 并查集)