题意:
经典的并查集:食物链
题解:
第一次做食物链的时候,《挑战》这本书上给的是一种称为:扩展域方法。
然而这种称为带权值的并查集可以开一个数组记录每个节点到根节点的权值。
这里的权值称为节点到根节点的“高度”。 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<string> #include<cstring> #include<vector> #include<functional> #include<utility> #include<set> #include<map> #include<cmath> using namespace std; const int maxn=50005; int pa[maxn]; int d[maxn];//与父节点的关系,0表示同类,1表示被吃,2表示吃父节点 int ans; int findset(int x) { int root; if(pa[x]==-1) return x; else { root=findset(pa[x]); d[x]=(d[x]+d[pa[x]])%3; return pa[x]=root; } } void unionset(int x,int y, int l) { int rootx=findset(x); int rooty=findset(y); if(rootx==rooty) { if((d[x]-d[y]+3)%3!=l) ans++; } else { pa[rootx]=rooty; d[rootx]=(d[y]-d[x]+l+3)%3; } } int main() { int n,m; cin>>n>>m; memset(pa,-1,sizeof(pa)); memset(d,0,sizeof(d)); while(m--) { int l,a,b; scanf("%d%d%d",&l,&a,&b); if(a>n||b>n||(l==2&&a==b))ans++; else unionset(a,b,l-1); } cout<<ans<<endl; }
