[并查集]LA 3644 X-Plosives
#include <cstdio> #include <cstring> const int INF=0x3f3f3f3f; const int MAXN=1e5+10; int parent[MAXN]; int findset(int a){ return parent[a]!=a ? parent[a] = find(parent[a]) :a ; } int main() { int a,b,ans=0,x,y; while(scanf("%d",&a)==1 ){ for(int i=0;i<MAXN;++i)parent[i]=i; ans=0; while(a!=-1){ scanf("%d",&b); x=findset(a);y=findset(b); if(x==y)ans++; else parent[y]=x; scanf("%d",&a); } printf("%d\n", ans); } return 0; }