poj1161 The Suspects
来源:互联网 发布:淘宝香港it代购真假 编辑:程序博客网 时间:2024/05/22 05:30
并查集基础题,没什么好理解的,纯粹是因为学kruskal而学的并查集
code:
/*ID: yueqiqPROG: numtriLANG: C++*/#include <set>#include <map>#include <ctime>#include <queue>#include <cmath>#include <stack>#include <limits>#include <vector>#include <bitset>#include <string>#include <cstdio>#include <cstring>#include <fstream>#include <string.h>#include <iostream>#include <algorithm>#define Si set<int>#define LL long long#define pb push_back#define PS printf(" ")#define Vi vector<int>#define LN printf("\n")#define lson l,m,rt << 1#define rson m+1,r,rt<<1|1#define SD(a) scanf("%d",&a)#define PD(a) printf("%d",a)#define SET(a,b) memset(a,b,sizeof(a))#define FF(i,a) for(int i(0);i<(a);i++)#define FD(i,a) for(int i(a);i>=(1);i--)#define FOR(i,a,b) for(int i(a);i<=(b);i++)#define FOD(i,a,b) for(int i(a);i>=(b);i--)#define readf freopen("input.txt","r",stdin)#define writef freopen("output.txt","w",stdout)const int maxn = 30001;const int INF = 0x3fffffff;const int dx[]={0,1,0,-1};const int dy[]={1,0,-1,0};const double pi = acos(-1.0);using namespace std;int N,M;int num[maxn],pre[maxn];void Make_Set(int k){ num[k]=1; pre[k]=k;}int Find_Set(int k){ if(k!=pre[k]){ pre[k]=Find_Set(pre[k]);//回溯赋值,压缩路径 } return pre[k];}//union the set of x,yvoid Union_Set(int x,int y){ x=Find_Set(x); y=Find_Set(y); if(x==y) return; if(num[x]>=num[y]){ pre[y]=x; num[x]+=num[y]; }else{ pre[x]=y; num[y]+=num[x]; }}int main(){ while(~scanf("%d%d",&N,&M) && (N||M)){ FF(i,N) Make_Set(i); int t,a,b; FF(i,M){ SD(t);SD(a); FOR(i,2,t){ SD(b); Union_Set(a,b); } }// FF(i,N){// if(i!=pre[i]){// printf("the s:%d pre:%d num:%d\n",i,pre[i],num[Find_Set(i)]);// }// } PD(num[Find_Set(0)]);LN; } return 0;}
- poj1161 The Suspects
- poj1161 The Suspects
- poj1161
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- Boost的转换函数polymorphic_cast和polymorphic_downcast
- Silverlight WCF RIA服务(十七)数据 7
- 关于JVM INSTR swap赋值
- Connect the Cities&&http://acm.hdu.edu.cn/showproblem.php?pid=3371
- Android Launcher抽屉类SlidingDrawer的使用
- poj1161 The Suspects
- spring入门十大问题解答
- ASP.NET和ASP相比的优缺点
- Silverlight WCF RIA服务(十八)共享代码 1
- Windows 7 64位搭建 PHP+apache+mysql环境
- Silverlight WCF RIA服务(十九)共享代码 2
- spring异常解决方法Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-
- 各位看过来–ViewPager在4.0下存在的兼容性问题
- zen-cart SEO优化相关文章