poj 1161 The Suspects
来源:互联网 发布:朱元璋北伐 知乎 编辑:程序博客网 时间:2024/05/19 14:02
题目链接:The Suspects
题目大意:给你一个n和m,代表有n个人,m个朋友团体,每个团体有k个人,朋友的朋友也是朋友,问最后跟0在一个朋友集合里面的人有多少个(包括0本身)
题目思路:并查集,直接连,连完之后判断就好
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <set>#include <map>#include <vector>using namespace std;int n,m,k,a,b,pre[100005];int Find(int x){ if(x == pre[x]) return x; return pre[x] = Find(pre[x]);}void join(int x,int y){ int p = Find(x); int q = Find(y); if(p != q) pre[q] = p;}int main(){ while(~scanf("%d%d",&n,&m)&&n+m){ for(int i = 0;i < n;i++) pre[i] = i; while(m--){ scanf("%d",&k); scanf("%d",&a); for(int i = 1;i < k;i++) scanf("%d",&b),join(a,b); } int ans = 0; for(int i = 0;i < n;i++){ if(Find(i) == Find(0)) ans++; } printf("%d\n",ans); } return 0;}
0 0
- poj 1161 The Suspects
- poj 1161 The Suspects
- POJ 1161 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 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
- java-springmvc+filter 替换输出流、response、响应内容
- 历届试题--兰顿蚂蚁
- JAVA集合相关基础知识
- 五一作业
- 【Unity3D】导入字体与淡入启动场景
- poj 1161 The Suspects
- Eclipse添加tomcat出现 The Apache Tomcat installation at this directory is version 8.5.6. A Tomcat 8.0 in
- 高精度模板汇总
- 苹果加速审核通道
- 经济学原理——微观经济学
- 学习能力与认知
- JavaScript中对象类型的汇总及相关方法的简介
- PyTorch实现DCGAN、pix2pix、DiscoGAN、CycleGAN、BEGAN VAE、Char RNN等
- 面向切面编程