点击打开链接

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want
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. 
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N.
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.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks. 
Sample Input
2
5 3
1 2
2 3
4 5 5 1
2 5
Sample Output
2
4

这个为什么TEL

Select Code

#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 (并查集)(水题)的更多相关文章

  1. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  4. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  5. 【PAT-并查集-水题】L2-007-家庭房产

    L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...

  6. POJ2524并查集水题

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  7. HDU1863(Kruskal+并查集水题)

    https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...

  8. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  9. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  10. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

随机推荐

  1. TMS Grid

    TMS Grid http://edn.embarcadero.com/article/42553

  2. LUA 运行期间不独占线程的递归,通过回调实现

    function main(d) local function func(d) moveto(d, function() print("d=======", d) d = d - ...

  3. avalon新一代UI库发布

    任何前端框架,尤其是国内的,想推广开,必须有一个UI库,光是一个核心库当光头司令是不行的.此外还有一个小圈子,供大家遇到问题时可以发问,一起完善.自从avalon嫁入"去哪儿网"后 ...

  4. Unix高级编程Note2

    [Unix Note2] 1.信号屏蔽 2.信号不会排队,即产生同时产生10次,会被合并为1次. 3.sigsuspend,sigsuspend后,进程就挂在那里,等待着开放的信号的唤醒.系统在接收到 ...

  5. S EAI 客户主数据导入_test(detail)

    一. 客户主数据模板导出 客户主数据和新联系人导入 Account_Config.ini文件 [Public] ConnectString=host="siebel://10.10.1.15 ...

  6. Visual Studio 2013 osg

    1>------ Rebuild All started: Project: ZERO_CHECK, Configuration: Debug x64 ------1> Checking ...

  7. mongodb查询速度慢是什么原因?

    mongodb查询速度慢是什么原因? 通过mongodb客户端samus代码研究解决问题         最近有项目需要用到mongodb,于是在网上下载了mongodb的源码,根据示例写了测试代码, ...

  8. IMU Noise Model

    1.参考资料2.相关定义3.IMU 的噪声模型3.1噪声的建模3.2白噪声和随机游走噪声的离散化3.3如何获取传感器噪声参数4.随机噪声和扰动的积分4.1建立模型4.2噪声的离散化模型推导4.3系统的 ...

  9. JAVA里的CAS算法简析

    Atomic 从JDK5开始, java.util.concurrent包里提供了很多面向并发编程的类. 使用这些类在多核CPU的机器上会有比较好的性能.主要原因是这些类里面大多使用(失败-重试方式的 ...

  10. c# 调用外包程序 等待处理完成结果

    string root = @"J:\yaoqianshu"; string pythonPath = "解压缩拷贝启动动画测试(新).py"; string ...