hdu 3635 并查集
来源:互联网 发布:hadoop java c 编辑:程序博客网 时间:2024/05/17 06:55
这道题主要还是要怎么判断矛盾,用rank数组保存与父亲节点的奇偶性,0表示相同,1表示不同,添加关系的时候先判断再更新就好了。。
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;int father[111111];int tim[111111],size[111111];int js=0;int find(int x){int t;if(father[x]!=x){t=find(father[x]);tim[x]=tim[father[x]]+tim[x];//累加转移次数 father[x]=t;}return(father[x]);}void tran(int a,int b){int x,y;x=find(a);y=find(b);if(x==y) return;father[x]=y;size[y]+=size[x];//个数加给父亲 tim[x]+=1;//根节点转移次数加1 }void query(int a){int t;t=find(a);printf("%d %d %d\n",t,size[t],tim[a]);}int main(){char ch;int t,n,m,i,j,a,b;scanf("%d",&t);while(t--){js++;printf("Case %d:\n",js);scanf("%d%d%",&n,&m);memset(tim,0,sizeof(tim));for(i=1;i<=n;i++){father[i]=i; size[i]=1;}for(i=1;i<=m;i++){cin>>ch;if(ch=='T'){scanf("%d%d",&a,&b);tran(a,b);}else{scanf("%d",&a);query(a);}}}}
0 0
- hdu 3635 并查集
- hdu 3635 并查集
- hdu 3635 并查集
- hdu 3635 并查集
- HDU 3635 并查集
- HDU 3635 Dragon Balls(并查集)
- hdu 3635 Dragon Balls(并查集)
- HDU 3635 Dragon Balls - 并查集
- hdu 3635 带权并查集
- HDU 3635 带权并查集
- hdu 3635 Dragon Balls 并查集
- HDU 3635 Dragon Balls(并查集)
- hdu-3635 Dragon Balls(并查集)
- HDU 3635 Dragon balls -并查集
- hdu Dragon Balls 3635【并查集】
- hdu 3635(并查集)
- HDU 3635 Dragon Balls(并查集)
- 带权并查集(hdu 3635,hdu 2818)
- HDU 5325 CRAZY BOBO 排序
- iOS 苹果官方Demo合集
- Scala GUI编程
- noip2010 乌龟棋
- linux udp 单播组播广播实现
- hdu 3635 并查集
- hdu 5326 Work【并查集】
- 2015年7月28日--博客开通
- 在Eclipse中关联源代码
- 母函数模板解释
- JS脚本无法调试
- java基础之集合函数-List
- HDU 5316 Magician
- Java学习之路0728(十二)(Class类、反射、Annotation功能注解)