How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 38888 Accepted Submission(s): 19371
Problem Description
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.
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
Author
Ignatius.L
题意:有n个朋友来参加生日派对,互相认识的人坐一桌(A认识B,B认识C,那么A就也认识C)。问需要几张桌子?
思路:朋友之间的关系可以看成图,那么需要几张桌子也就是求图的连通分量,只需要用并查集来找出有几个根节点即可。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int pre[1005];
int total;//根节点个数
int Find(int x)
{
int r=x;
//寻找根节点
while(r!=pre[r])
r=pre[r];
//路径压缩
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
//如果两个点的根节点不同,那么这两拨人可以合在一起,根节点减一
if(fx!=fy)
{
pre[fx]=fy;
total--;
}
}
int main()
{
int T,n,m,i,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
//刚开始所有人的根节点都是自身
for(i=0;i<=n;i++)
pre[i]=i;
total=n;
while(m--)
{
scanf("%d %d",&a,&b);
join(a,b);
}
printf("%d\n",total);
}
return 0;
}
附上java代码:
import java.util.Scanner;
public class Main {
static int[] pre=new int[1005];
static int total;
public static int Find(int x) {
int r=x;
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(i!=r) {
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
public static void join(int x,int y) {
int fx=Find(x);
int fy=Find(y);
if(fx!=fy) {
pre[fx]=fy;
total--;
}
}
public static void main(String arg[]) {
Scanner in=new Scanner(System.in);
int n,m,T;
T=in.nextInt();
while(T>0) {
T--;
n=in.nextInt();
m=in.nextInt();
total=n;
for(int i=1;i<=n;i++)
pre[i]=i;
while(m>0) {
m--;
int a=in.nextInt();
int b=in.nextInt();
join(a,b);
}
System.out.printf("%d\r\n",total);
}
}
}