易炸物(LA 3644)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100000 + 10;
int f[maxn];

int find(int x) {
	return f[x] != x ? f[x] = find(f[x]) : x;
}



int main() {
	int x, y;
	while(scanf("%d", &x) == 1) {
		for(int i = 0; i <= maxn; i++) {
			f[i] = i;
		}
			int refuse_num = 0;
			while(x != -1) {
				scanf("%d", &y);
				x = find(x); y = find(y);
				if(x == y) {
					++ refuse_num;
				}
				else{
					f[x] = y;
				} 
				scanf("%d", &x);
			}
			printf("%d\n", refuse_num);
		}
	}


你可能感兴趣的:(易炸物(LA 3644))