POJ
来源:互联网 发布:喝咖啡的利弊 知乎 编辑:程序博客网 时间:2024/04/30 08:45
思路:求与0连通的学生数,包含0自身。
AC代码:
#include <cstdio>#include <cmath>#include <cctype>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10#define inf 0x3f3f3f3f#define PI pair<int, int> typedef long long LL;const int maxn = 30000 + 5;int p[maxn];int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}void join(int x, int y) {int px = find(x), py = find(y);if(px != py) p[px] = py;}int main() {int n, m;while(scanf("%d%d", &n, &m) == 2 && n+m) {for(int i = 0; i < n; ++i) p[i] = i;int num, x, y;for(int i = 0; i < m; ++i) {scanf("%d%d", &num, &x);for(int j = 1; j < num; ++j) {scanf("%d", &y);join(x, y);x = y;}}int goal = find(0);int ans = 0;for(int i = 0; i < n; ++i) {if(find(i) == goal) ++ans;}printf("%d\n", ans);}return 0;}
如有不当之处欢迎指出!
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- UVALive 4225 Prime Bases 贪心
- gmapping链接收集
- ACM程序设计题目 Problem M-13
- Sublime Text 设置
- 栈的链式存储结构操作及实现
- POJ
- [oracle]Ubuntu下ORA-27102及ORA-00845错误处理
- 实习 day5
- 剑指Offer 11 数值的整数次方
- View、自定义View
- LocakOption 参数
- L1-002. 打印沙漏
- java swing入门 计算器布局
- 编译及调试OpenJDK 8