#include <cstdio>
using namespace std;
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
const int maxn = 1E5 + 100;
int fa[maxn];
void ini(){
FOR(i,0,maxn) fa[i] = i;
}
int fnd(int x){
return x == fa[x]?x:fa[x] = fnd(fa[x]);
}
int uni(int x,int y){
x = fnd(x);y = fnd(y);
if(x == y) return 0;
fa[x] = y;
return 1;
}
int main(){
int u,v;
while(~scanf("%d",&u)){
int ans = 0;
ini();
while(~u){
scanf("%d",&v);
if(!uni(u,v)) ++ans;
scanf("%d",&u);
}
printf("%d\n",ans);
}
return 0;
}
[2016-03-19][UVALive][3644][X-Plosives]
原文:http://www.cnblogs.com/qhy285571052/p/b3dd6cec2366a8c90f971ba32245c5d6.html