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类). 把两个元素放在一个组代表 ...
随机推荐
- C#中DateTime详解
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- mysql 搜索字段不包含数字
select a.REG_CN_NAME,a.REG_NUM,a.INT_CLS from T_FENFA_43_TM_HOLDERINFO a where a.REG_CN_NAME NOT RE ...
- 01-A-1: 计算
- win8上部署.net4.0程序到iis
在win8.1上默认的iis版本为8.5版,不做任何配置回报3个错误, 一下是错误提示内容及解决方案 1>HTTP 错误 404.3 – Not Found由于扩展配置问题而无法提供您请求的页面 ...
- C语言日志处理
一.简介 zlog是一个高可靠性.高性能.线程安全.灵活.概念清晰的纯C日志函数库,在效率.功能.安全性上大大超过了log4c,并且是用c写成的,具有比较好的通用性. 二.安装 下载 https:// ...
- ubuntu 卡在登陆界面无法进入桌面,但是可以进入命令行界面
ubuntu 卡在登陆界面无法进入桌面,但是可以进入命令行界面(初步断定是Xwindows界面软件出问题了,所以重装即可!)Solve: 1.Ctrl+Alt+F1进入命令行界面,root账户登陆2. ...
- 第十届国际用户体验创新大赛携Mockplus走进校园
今日立夏,万木并秀,生生不息,第十届国际用户体验创新大赛即将拉开序幕.5月5日下午,一场用户体验设计经验分享活动携带众多嘉宾降临成都理工大学,为西南赛区首站赛事宣讲. 本次宣讲活动邀请了华为技术有限公 ...
- jdbc注册驱动 class.forName()
从源码 D:\Javasoftware\MySql\mysql\mysql-connector-java-5.1.7\src\com\mysql\jdbc\Driver.java class.forN ...
- openssl生成ssl证书(转)
原文:http://blog.sina.com.cn/s/blog_4fd50c390101891c.html x509证书一般会用到三类文,key,csr,crt. Key 是私用密钥openssl ...
- Nginx搭建后,图片存储在Tomcat上,前端无法回显图片问题
一.Nginx与Tomcat连接搭建的环境,Nginx设置了前端的访问路径为 (1)前端代码配置: root /usr/local/nginx/html; index index.html index ...