poj-1703 Find them, Catch them!! 并查集
来源:互联网 发布:linux tcpdump 用法 编辑:程序博客网 时间:2024/06/06 11:04
题意就不多说了,要注意一下当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;}/*111 30A 1 2Not sure yet.D 1 3D 4 5A 1 4Not sure yet.D 3 5A 1 4In different gangs.A 1 5In the same gang.D 3 5D 7 9D 2 7A 1 2Not sure yet.D 1 7A 1 9In the same gang.A 3 7In the same gang.A 5 7In different gangs.A 2 5In the same gang.A 2 4In different gangs.A 9 5In the same gang.D 8 10D 6 8A 6 10In the same gang.D 10 11A 8 11In the same gang.D 1 8A 1 11In different gangs.A 4 11In the same gang.A 9 11In different gangs.A 2 6In the same gang.A 3 6In different gangs.A 1 1<pre name="code" class="cpp">In the same gang.14 4D 1 2D 3 4D 1 3A 1 4<pre name="code" class="cpp">In the same gang.下面是在网上找的大神的测试样例.
0 0
- POJ 1703 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 //并查集
- POJ 1703 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(并查集)
- poj 1703 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 种类并查集
- POJ 1703 Find them, Catch them (关系并查集)
- POJ-1703 Find them, Catch them 经典并查集
- poj 1703 Find them,Catch them 并查集
- live555学习-ByteStreamFileSource和H264VideoStreamFramer
- 用MFC实现串口编程
- 青岛首次公布户外广告禁设区 楼顶“高炮"被限高
- json对象的遍历
- 解决“您尝试打开的文件*.xls的格式与文件扩展名指定的格式不一致”
- poj-1703 Find them, Catch them!! 并查集
- Java获取项目中的路径
- uitableview
- PXE-E53:NO boot filename received解决办法
- HDU-#4263 Red/Blue Spanning Tree(Kruskal)
- mantis 流程页面反映慢
- 程序锁
- c练习题1:求最大公约数,最小公倍数
- srand ()与rand()函数小应用之猜数游戏