D - How Many Tables (并查集)(水题)
to stay with strangers.
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.
Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
2
5 3
1 2
2 3
4 5 5 1
2 5
2
4
这个为什么TEL
#include <stdio.h>
#include <string.h>
#define Maxn 1005
int par[Maxn],count=0;
void init(int n)
{
for(int i=0; i<n; i++)
par[i]=i;
}
int find(int x)
{
int r=x;
while(par[r]!=r)
{
r=par[r];
}
int i=x,j;
while(i!=r)
{
j=par[r];
par[r]=i;
i=j;
}
return r;
} void join(int x, int y)
{
int fx=find(x), fy=find(y);
if(fx!=fy)
{
par[fx]=fy;
count++;
}
}
int main()
{
int i,j,a,b,n;
scanf("%d",&n);
while(n--)
{
count=0;
scanf("%d%d",&a,&b);
init(a);
while(b--)
{
scanf("%d%d",&i,&j);
join(i,j);
}
printf("%d\n",a-count);
} return 0;
}
AC代码
Select Code
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int pre[1100];
int findset(int v)
{
int t1,t2=v;
while(v!=pre[v]) v=pre[v];
while(t2!=pre[t2])
{
t1=pre[t2];
pre[t2]=v;
t2=t1;
}
return v;
}
void unions(int x,int y)
{
int t1=findset(x);
int t2=findset(y);
if(t1!=t2) pre[t1]=t2;
}
int main()
{
int T,n,m;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) pre[i]=i;
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
unions(u,v);
}
int ans=0;
for(int i=1;i<=n;i++)
if(pre[i]==i) ans++;
cout<<ans<<endl;
}
}
D - How Many Tables (并查集)(水题)的更多相关文章
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- poj2524(并查集水题)
题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...
- 【PAT-并查集-水题】L2-007-家庭房产
L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
- HDU1863(Kruskal+并查集水题)
https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
随机推荐
- MySQL函数及用法示例
(一) 1.字符串函数ascii(str) 返回字符串str的第一个字符的ascii值(str是空串时返回0) mysql> select ascii('2'); -> 50 ...
- VB中的正则表达式
RegExp对象提供简单的正则表达式支持功能. RegExp对象的用法:Function RegExpTest(patrn, strng)Dim regEx, Match, Matches ' 建立变 ...
- TNS:no listener error in Oracle XE after changing computer name
This morning at work when trying to log on to my computer I noticed not my username on login screen ...
- linux rz sz 的安装
先wget获取包wget http://freeware.sgi.com/source/rzsz/rzsz-3.48.tar.gz然后解压tar zxvf rzsz-3.48.tar.gz进入目录c ...
- 【335】Install PyDev in Eclipse IDE
Reference: Eclipse和PyDev搭建完美Python开发环境(Windows篇) Reference: Install the PyDev plug-in for Eclipse Do ...
- 解决docker tty窗口太小,命令换行的问题
docker exec -it -e LINES=$(tput lines) -e COLUMNS=$(tput cols) ed08 bash
- 学习 java 编程语言两个月来的感受
今天是我接触java编程的第81天, 现在的感受是觉得非常快乐, 每次看着自己的写的代码能成功运行的时候那种喜悦是无法言表的, 最初接触电脑还是在小学, 后来到了初中觉得电脑无非就是点点鼠标, 按按键 ...
- Python all() 函数
Python all() 函数 Python 内置函数 描述 all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False. ...
- 区块链相关在线加解密工具(非对称加密/hash)
https://cse.buffalo.edu/blockchain/tools.html https://cse.buffalo.edu/blockchain/encryption.html 由纽约 ...
- HTTP Cookie 详解
参考: http://blog.csdn.net/lijing198997/article/details/22174151