poj-1703 Find them, Catch them!! 并查集
2014-08-14 10:16
411 查看
题意就不多说了,要注意一下当N=2时一定是属于两个不同帮派的,而不是不确定。
看起来很简单的并查集,我愣是作了一晚上,最后才发现递归写错了
。
思路大体是这样的,对每一对并在一起的犯罪团伙,记录下他与根节点的距离,当询问时,只要看看这两个的根节点是否相同就能判断是否已经确定,让后根据与根节点的距离来判断是否是同一帮派
下面是在网上找的大神的测试样例.
看起来很简单的并查集,我愣是作了一晚上,最后才发现递归写错了
。
思路大体是这样的,对每一对并在一起的犯罪团伙,记录下他与根节点的距离,当询问时,只要看看这两个的根节点是否相同就能判断是否已经确定,让后根据与根节点的距离来判断是否是同一帮派
#include<stdio.h> #include<string.h> struct s { int fa; int val; }; s map[100010]; int fi(int x) //递归寻找根节点的过程中要更新所有关联的节点 { if(map[x].fa==x) return x; int temp=map[x].fa; map[x].fa=fi(map[x].fa); map[x].val=(map[temp].val+map[x].val)%2; return map[x].fa; } int main() { int T,n,m,i,x,y,flag,cnt1,cnt2; char c; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); if(n==2) { for(i=1; i<=m; i++) { getchar(); scanf("%c%d%d",&c,&x,&y); if(c=='A') { if(x==y) printf("In the same gang.\n"); else printf("In different gangs\n"); } } } else { for(i=1; i<=n; i++) { map[i].fa=i; map[i].val=0; } for(i=1; i<=m; i++) { getchar(); scanf("%c%d%d",&c,&x,&y); if(c=='D') { cnt1=fi(x); cnt2=fi(y); if(cnt1<cnt2) { map[cnt2].fa=cnt1; map[cnt2].val+=(map[y].val+1+map[x].val); //记下与根节点的距离 } else { map[cnt1].fa=cnt2; map[cnt1].val+=(map[y].val+map[x].val+1); } // for(int j=1;j<=n;j++) // { // printf("%d %d %d\n",j,map[j].fa,map[j].val); // } } if(c=='A') { cnt1=x; cnt2=y; x=fi(x); y=fi(y); cnt1=map[cnt1].val; cnt2=map[cnt2].val; // for(int j=1;j<=n;j++) // { //printf("%d %d %d\n",j,map[j].fa,map[j].val); // } if(x==y) { if((cnt1%2)==(cnt2%2)) printf("In the same gang.\n"); else printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } } } } return 0; } /*1 11 30 A 1 2 Not sure yet. D 1 3 D 4 5 A 1 4 Not sure yet. D 3 5 A 1 4 In different gangs. A 1 5 In the same gang. D 3 5 D 7 9 D 2 7 A 1 2 Not sure yet. D 1 7 A 1 9 In the same gang. A 3 7 In the same gang. A 5 7 In different gangs. A 2 5 In the same gang. A 2 4 In different gangs. A 9 5 In the same gang. D 8 10 D 6 8 A 6 10 In the same gang. D 10 11 A 8 11 In the same gang. D 1 8 A 1 11 In different gangs. A 4 11 In the same gang. A 9 11 In different gangs. A 2 6 In the same gang. A 3 6 In different gangs. A 1 1 <pre name="code" class="cpp">In the same gang. 1 4 4 D 1 2 D 3 4 D 1 3 A 1 4 <pre name="code" class="cpp">In the same gang.
下面是在网上找的大神的测试样例.
相关文章推荐
- 【poj 1703 Find them, Catch them + 并查集】
- Find them, Catch them POJ - 1703(并查集,模板)
- poj1703 Find them, Catch them——带权并查集
- POJ1703 Find them, Catch them
- poj 1703 Find them,Catch them
- poj-1703 find them,catch them!
- poj-1703 Find them, Catch them ***
- poj 1703 : Find them Catch them (并查集)
- POJ1703 Find them, Catch them
- Find them, Catch them poj 1703
- 北大ACM1703——Find them, Catch them~~并查集
- 刷题——Find them, Catch them POJ - 1703
- Find them, Catch them POJ - 1703
- POJ 1703 Find them, Catch them
- POJ 1703 Find them, Catch them .
- POJ 1703 Find them, Catch them
- Find them, Catch them poj 1703
- Find them, Catch them POJ - 1703
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 Find them, Catch them(并查集应用)