The Suspects 并查集练习
来源:互联网 发布:修改表数据的sql语句 编辑:程序博客网 时间:2024/06/11 08:38
题目地址:https://vjudge.net/problem/POJ-1611
题目大意:最近SARS传播, 学校里为了隔离可能被感染的人, 让你找出个能被感染的人, 现规定, 在同一个社团中, 只要有一人有感染嫌疑, 则所有人都有感染嫌疑, 已知0号学生是感染嫌疑者,现在给你学校里有n个学生, m个社团,输入第一行是n 和 m, 之后的m行, 先给出这个社团的成员人数, 再给出成员的学号。
解题思路:把社团里的成员都并到这个这团的第一个人身上, 如果社团中有0,就把第一个并到0上。(有点麻烦, 毕竟我还是萌新)
ac代码
#include <cstdio>#include <cstring>#include <iostream>#include <string>using namespace std;const int MAX = 30010;int stu[MAX];int n, m, ans;void makeset(int n){ for(int i = 0; i < n;i++) stu[i] = i;}int finding(int x){ if(x != stu[x]) stu[x] = finding(stu[x]); return stu[x];}void bing(int x, int y){ if(finding(x) != finding(y)) stu[finding(x)] = finding(y);}int main(){ int a, b, first, out; while(scanf("%d%d", &n, &m) != EOF) { makeset(n); out = 0; if(n == 0 && m == 0) break; if(n == 1 && m == 0) { cout << 1 << endl; continue; } while(m--) { cin >> a >> first; for(int i = 0; i < a - 1; i++) { cin >> b; if(b == 0) stu[finding(first)] = b; else if(finding(first) != 0 && finding(b) != 0) bing(first, b); else if(finding(first) == 0) stu[finding(b)] = finding(first); else if(finding(b) == 0) stu[finding(first)] = finding(b); } } for(int i = 0 ; i < n; i++) { if(finding(stu[i]) == 0) {out++;} } cout << out << endl; } return 0;}
阅读全文
0 0
- The Suspects 并查集练习
- 【POJ 1611】 The Suspects(并查集练习)
- The Suspects 并查集
- The Suspects(并查集)
- 并查集 The Suspects
- The Suspects 并查集
- 并查集 - The Suspects
- The Suspects(并查集)
- 并查集----The Suspects
- The Suspects 并查集
- The Suspects -- 并查集
- 1611 The Suspects 并查集
- POJ 1611 The Suspects(并查集)
- POJ1611 The Suspects 并查集求解
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- POJ1611 The Suspects 并查集
- zoj 1789The Suspects 并查集
- 使用dotTrace6.0进行性能,内存分析
- firewall记录
- 上传非结构化数据到HBase
- 工作环境搭建
- m3u8参数详解
- The Suspects 并查集练习
- 自定义注解实现Excel的解析生成
- Python学习笔记:汉字字频统计
- Web Service
- 大数据等最核心的关键技术:32个算法
- WatchOS系统开发大全(1)-AppleWatch背景介绍
- 光流(一)--综述概览
- Oracle trunc()函数的用法
- undistortPoints opencv