题目链接

http://poj.org/problem?id=1611

题意

给出 n, m

有n个人 编号为 0 - n - 1

有m组人 他们之间是有关系的

编号为 0 的人是 有嫌疑的

然后和 编号为0的人有关系 或者 和 编号为0的人有关系的人 有关系的 都是有嫌疑的

找出共有多少人有嫌疑

思路

将所有有关系的人 合并 然后 去查找 所有的人的根节点 如果和编号为0的人的根节点 相同 他就是有关系的

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7; int pre[maxn]; int find(int x)
{
int r = x;
while (pre[r] != r)
r = pre[r];
int i = x, j;
while (i != r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
} void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (x != fy)
pre[fx] = fy;
} void init()
{
for (int i = 0; i < maxn; i++)
pre[i] = i;
} int main()
{
int n, m;
while (scanf("%d%d", &n, &m) && (n || m))
{
init();
int Max = -INF;
for (int i = 0; i < m; i++)
{
int k;
int ini, num;
scanf("%d%d", &k, &ini);
Max = max(Max, ini);
for (int i = 1; i < k; i++)
{
scanf("%d", &num);
Max = max(num, Max);
join(ini, num);
}
}
int ans = 1;
int vis = find(0);
for (int i = 1; i <= Max; i++)
{
if (find(i) == vis)
ans++;
}
cout << ans << endl;
}
}

最新文章

  1. Scalaz(57)- scalaz-stream: fs2-多线程编程,fs2 concurrency
  2. npm 安装不了模块
  3. np2016课程总结
  4. iptables 端口转发
  5. UITableView代理方知多少+执行顺序
  6. getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect
  7. Solr的安装
  8. 用continue语句的时候,要千万小心内存泄漏,当然还有return和break也是
  9. 对于deferred的一点点理解
  10. 【转】linux ls -l的详解
  11. [LeetCode] BFS解决的题目
  12. C#应用编程小例子-03-展示另一个窗体
  13. sqoop碰到的问题
  14. EJB3.0之事务
  15. Spring 注解(一)Spring 注解编程模型
  16. BZOJ 4764: 弹飞大爷
  17. 前端学习 -- Css -- 否定伪类
  18. Pycharm 2017 激活码
  19. Mac OS 10.12 - 安装任何来源软件!!
  20. Linux内核分析4

热门文章

  1. Taskaffinity属性使用小结
  2. EasyMvc入门教程-高级控件说明(15)方位布局控件
  3. newlisp HTTP Basic Authentication
  4. RPM命令使用
  5. Linux学习之十九-Linux磁盘管理
  6. JNI开发(2)——开发实战
  7. Solidworks如何使用Toolbox
  8. struts2学习笔记2 -struts2的开发步骤和工作原理
  9. TCP/IP详解 卷一(第七、八章 Ping、Traceroute程序)
  10. 封装GetQueryString()方法来获取URL的value值