并查集-poj-1611-The Suspectst
来源:互联网 发布:淘宝跳转链接代码 编辑:程序博客网 时间:2024/06/08 10:13
题目的意思是有0到n个人,0号感染病毒,数据有m组每组的人只要有一个就所有人都会被感染。问一共有几个人被感染。
简单的并查集,fa表示种类,num表示该种类的个数,不包括父节点,找的时候只要找到根就好。
#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int fa[33333];int num[33333];int find_fa(int n) { return fa[n]==n?n:find_fa(fa[n]);}void inti(int n) { for(int i=0;i<=n;i++) { fa[i]=i; num[i]=1; }}int main(){ int n,m; while(scanf("%d%d",&n,&m)&&(m+n)!=0) { inti(n); int x,a; for(int i=1;i<=m;i++) { scanf("%d",&x); scanf("%d",&a); int fx=find_fa(a); int tt; for(int j=2;j<=x;j++) { scanf("%d",&tt); int fy=find_fa(tt); if(fx!=fy) { fa[fy]=fx; num[fx]+=num[fy]; } } } int sss=find_fa(fa[0]); cout << num[sss] << endl; } return 0;}
0 0
- 并查集-poj-1611-The Suspectst
- POJ 1611 The Suspects(并查集)
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- POJ 1611 The Suspects 并查集
- POJ--1611--The Suspects【并查集】
- poj 1611-The Suspects:并查集
- POJ--1611 -- The Suspects [并查集]
- poj 1611 - The Suspects(并查集)
- poj 1611The Suspects 并查集
- poj 1611 The Suspects 并查集
- [并查集]POJ 1611 The Suspects
- poj 1611 The Suspects --- 并查集
- POJ 1611 The Suspects (并查集)
- POJ 1611 The Suspects(并查集)
- POJ 1611--The Suspects 并查集
- POJ 1611 The Suspects 并查集
- 第二周项目4-用循环求
- 框架中的单例模式
- [Leetcode]Sudoku Solver
- Visual C++中 #include "stdafx.h" 头文件的用法
- 排序算法总结 java实现
- 并查集-poj-1611-The Suspectst
- 用信号量解决进程的同步与互斥
- android5.0中图片跳转的动画效果
- vs2010程序运行出错 link : fatal error lnk1123: 转换到 coff 期间失败: 文件无效或损坏
- 第二周项目5-用循环求.2
- 读书笔记-《统计思维 程序员数学之概率统计》
- 求素数
- 蓝桥杯 六角填数 (next_permutation/新技能)
- 记第一次电面