poj2492 A Bug's Life 种类并查集
2014-05-14 17:16
495 查看
Language: Default A Bug's Life
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. Problem Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. Input The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one. Output The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong. Sample Input 2 3 3 1 2 2 3 1 3 4 2 1 2 3 4 Sample Output Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found! Hint Huge input,scanf is recommended. Source TUD Programming Contest 2005, Darmstadt, Germany |
思路:开一个rank数组表示其与父亲节点的关系。rank[x]=0表示x与fa[x]为同性,rank[x]=1,表示x与fa[x]为异性。rank在findset中更新时,很明显可以发现,rank[x]=(rank[x]+rank[fa[x]])%2,不过注意此时fa[x]是未更新前的fa[x]。 在Union操作中,我们需要更新fx,fy,我们发现rank[fy]=(rank[x]+rank[y]+1)%2,rank[fx]=(rank[x]+rank[y]+1)%2,则利用上面的公式更新即可。关于判断,当x,y属于同一个根的时候,如果rank[x]=rank[y],即fx=fy时,我们由rank[x]=rank[y]可推出x和y同性,那么就说明存在bug。详见代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=2000+100; int fa[MAXN],rank[MAXN]; int n,m; void init() { for(int i=1;i<=n;i++) fa[i]=i; memset(rank,0,sizeof(rank)); } int findset(int x) { if(fa[x]!=x) { int root=fa[x]; fa[x]=findset(fa[x]); rank[x]=(rank[x]+rank[root])%2; } return fa[x]; } void Union(int x,int y) { int fx=findset(x); int fy=findset(y); fa[fy]=fx; rank[fy]=(1+rank[y]+rank[x])%2; } int main() { //freopen("text.txt","r",stdin); int T,kase=0; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); kase++; init(); int flag=0; for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); if(flag) continue; int fx=findset(x); int fy=findset(y); if(fx==fy) { if(rank[x]==rank[y]) flag=1; } else Union(x,y); } printf("Scenario #%d:\n",kase); if(flag) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); if(T) printf("\n"); } return 0; }
相关文章推荐
- POJ 2492 A Bug's Life(种类并查集 + 偏移量)
- poj 2492 A Bug's Life(种类并查集)
- POJ - 2492 A Bug's Life(种类并查集)
- Hdu 1892&&Poj 2492 A Bug's Life[判断二分图 || 种类并查集]
- poj2492 A Bug's Life(并查集)
- poj 2492 A Bug's Life(带权并查集)
- poj 2492 A Bug's Life(并查集)分组并查集
- HDU 1829 && POJ 2492 A Bug's Life(种类并查集)
- POJ 2492 A Bug's Life [数据结构-并查集 union-find sets]
- POJ 2492 A Bug's Life(加权并查集)
- POJ 2492 A Bug's Life(扩展并查集)
- POJ 2492 A Bug's Life(并查集)
- poj2492——A Bug's Life(并查集扩展)
- POJ 2492 A Bug's Life(路径压缩并查集)
- [POJ 2492] A Bug's Life [扩展并查集]
- poj 2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life (并查集)
- POJ 2492 A Bug's Life (并查集)
- hdu 1829 &poj 2492 A Bug's Life(判断二分图、带权并查集)
- poj 2492 A Bug's Life(带权并查集)