hdu 1213
来源:互联网 发布:php memcache set参数 编辑:程序博客网 时间:2024/06/05 09:05
//并查集求连通分量的个数#include <iostream>#include<cstdio>#include<cstring>#include<set>using namespace std;const int maxn=1000+5;int parent[maxn];int ran[maxn];int N,M; //N为朋友的个数 M为关系个数set<int>d;int find(int i){ if(i!=parent[i]) parent[i]=find(parent[i]); return parent[i];}void unite(int x,int y){ int p1=find(x); int p2=find(y); if(p1!=p2) parent[p2]=p1;}int main(){ int T; scanf("%d",&T); while(T--) { d.clear(); scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) { parent[i]=i; ran[i]=1; } while(M--) { int v1,v2; scanf("%d%d",&v1,&v2); unite(v1,v2); } for(int i=1;i<=N;i++) d.insert(find(i)); cout<<d.size()<<endl; } return 0;}
阅读全文
0 0
- HDU 1213
- HDU 1213
- HDU 1213
- HDU 1213
- HDU 1213
- hdu 1213
- hdu 1213
- HDU 1213
- HDU 1213
- hdu 1213
- hdu 1213
- hdu 1213
- hdu 1213
- hdu 1213
- hdu(1213)
- HDU 1213
- hdu 1213
- HDU 1213
- 对比Ubuntu与Win10的资源占用
- java.lang.StackOverflowError错误
- Spring之BeanFactory 与 FactoryBean
- Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be
- 20171110
- hdu 1213
- 找最高分
- 【编译原理练习题】由字母表求字符串个数
- 调研NFV编排工具(by quqi99)
- Maven中pom.xml详解
- application.properties与application.yml之间的区别
- 硬盘的那些事(主分区、扩展分区、逻辑分区、活动分区、系统分区、启动分区、引导扇区、MBR等
- 浅谈 Mybatis 中的 ${ } 和 #{ }的区别
- 用函数编程计算两整数的最大值