例题2
来源:互联网 发布:chrome linux版 编辑:程序博客网 时间:2024/05/22 05:58
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33186 Accepted Submission(s): 16562
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
25 31 22 34 55 12 5
24
Eddy
代码:
#include <iostream>
using namespace std;
int par[1002];
void chushi(int n){
for(int i=0;i<n;i++)
par[i]=i;
}
int gen(int x){
return par[x]==x?x:gen(par[x]);
}
int jiehe (int x,int y){
x=gen(x);
y=gen(y);
if(x!=y){
par[x]=y;
}
}
int main()
{
int a;
cin>>a;
if(a<=0){
return 0;
}
if(a>0){
for(int i=0;i<a;i++){
int n,m;
int x,y;
cin>>n>>m;
if(n==0){
cout<<"0"<<endl;
}
else if(n==1){
cout<<"1"<<endl;
}
else {
chushi(n);
for(int j=0;j<m;j++){
cin>>x>>y;
jiehe(x,y);
}
int sum=0;
for(int k=0;k<n;k++){
if(k==par[k])
sum++;
}
cout<<sum<<endl;
}
}
return 0;
}
}
- 例题2
- 例题2-2
- 例题2-2
- C语言例题2:
- 例题1--2
- 例题2--3--3
- 例题3-2
- 例题5.2.2
- 例题5.3.2
- 例题5.4.2
- 例题6.1.2
- 例题6.3.2
- 例题6.4.2
- 例题7.4.2
- 例题2-1
- 例题2-3
- 例题2-4
- 例题3-2
- Qt打开指定目录并选中指定文件
- 11-13springmvc实现json交互
- python小爬虫—抓取pixabay网站的图片资源
- C++向上转型
- 160个crackme之008
- 例题2
- 【拓展欧几里得】方程的解
- matlab求解全局最优(初步介绍)
- 用飞鱼星路由器上网时能联通VPN但不能连接到具体的服务器:
- 【jzoj5232】【NOIP2017模拟A组模拟8.5】【带权排序】【线段树】
- array_merge() 函数的用法
- 关于glibc的system函数调用实现
- three.js 开发中遇到的坑
- 用npm离线安装webpack