poj 1611
来源:互联网 发布:note3支持4g网络吗 编辑:程序博客网 时间:2024/06/01 08:05
题意:输入n,m分别表示学生人数和组数,然后输入m组数据,现在已经知道如果一个小组里面的一个人得病,那么该小组都有可能得病。而且已经知道0号学生已经得病,问:一共有多少人可能得病?
解法:并查集
用树高代表集合的大小,最后输出根为0的小组的人数就可以了!
#include <iostream>#include<cstdio>#define max_n 30005int father[max_n];int Rank[max_n];int a[max_n];using namespace std;void start_union(int n){for (int i = 0; i <= n-1; i++){father[i] = i;Rank[i] = 1;}}int find(int r){while (father[r] != r){r = father[r];}return r;}void join(int x, int y){int fx = find(x);int fy = find(y);if (fx == fy) return;if (Rank[fx] >= Rank[fy]){father[fy] = fx;Rank[fx] += Rank[fy];}else{father[fx] = fy;Rank[fy] += Rank[fx];}}int main(){int n,m;while (scanf_s("%d%d", &n, &m) != EOF){if (n == 0 && m == 0) break;start_union(n);while (m--){int t;scanf_s("%d", &t);// memset(a,0,sizeof(a));for (int i = 0; i<t; i++){scanf_s("%d", &a[i]);if (i != 0) join(a[i - 1], a[i]);}}printf_s("%d\n", Rank[find(0)]);}return 0;}
0 0
- POJ 1611
- POJ 1611
- poj 1611
- poj-1611
- poj 1611
- poj 1611
- poj 1611
- POJ 1611
- poj 1611
- POJ 1611
- poj--1611
- poj 1611
- poj-1611
- poj 1611
- poj-1611
- poj 1611
- poj 1611
- poj 1611
- Android studio加载网络权限
- 理解 JavaScript 中的 Function.prototype.bind - 文章 - 伯乐在线
- 利用C++制作dll并调用dll
- 广义表的各种运算
- BP神经网络
- poj 1611
- 【jcp jsr】 JCP JSR 认识
- node更新版本后sass无法编译解决办法
- 在Fedora 22 上安装VirtualBox-5.0的问题解答
- Hadoop2.2.0伪分布式之MapReduce简介
- 浅谈设计模式之组合模式
- ORA-12916 cannot shrink permanent or dictionary managed tablespace
- eclipse修改注释中的@author和格式化
- jquery的一个插件scrollable.js做的注册三步骤,只有完成第一个才能进入下一步