1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include<bits/stdc++.h> using namespace std; enum{N=300010}; int n,m,i,u,v,w,F[N],S[N]; int Find(int x){ return F[x]==x?x:F[x]=Find(F[x]); } map <int,int> M[N]; void U(int x,int y){ x=Find(x),y=Find(y); if(x==y) return; if(M[x].size()>M[y].size()) swap(x,y); F[x]=y,S[y]+=S[x]; for(auto i:M[x]) M[y].emplace(i); for(auto i:M[x]) U(M[y][i.first],i.second); } main(){ for(scanf("%d%d%*d",&n,&m),i=1;i<=n;++i) S[F[i]=i]=1; while(m--){ scanf("%d%d%d",&v,&u,&w),u=Find(u),v=Find(v); if(M[u].count(w)) U(M[u][w],v); else M[u][w]=v; } int64_t ans=0; for(i=1;i<=n;++i) if(Find(i)==i) ans+=1ll*S[i]*(S[i]-1)/2; printf("%lld\n",ans); }
|