一、Vjudge
1.How Many Tables
题意:
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.
输入:
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.
输出:
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
样例输入:
2
5 3
1 2
2 3
4 5
5 1
2 5
样例输出:
2
4
解题思路:
这就是这周学的典型的并查集的问题,大意就是让认识的人坐一桌,问分为几桌的问题。其实就是用s数组来记录他们的关系,直到s[i]=s,就找到了最后一个结点。所以最后有几个s[i]=i,就有几桌,要是不清楚可以画一张图来发现一下规律。
刚开始交了两遍一直TLE,最后才发现是因为找x递归的时候,一直是x,应该是s[x]才对,改过来就对了。
程序代码:
#include<iostream>
using namespace std;
const int N=1000+5;
int s[N];
int find_x(int x){
return x==s[x]?x:find_x(s[x]);
}
void union_tree(int x,int y){
x=find_x(x);
y=find_x(y);
if(x!=y)
s[x]=s[y];
}
int main(){
int T;
int x,y;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
s[i]=i;
}
for(int i=1;i<=m;i++){
scanf("%d %d",&x,&y);
union_tree(x,y);
}
int cnt=0;
for(int i=1;i<=n;i++){
if(s[i]==i)
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
2.Ubiquitous Religions
题意:
There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.
You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.
输入:
The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.
输出:
For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.
样例输入:
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
样例输出:
Case 1: 1
Case 2: 7
解题思路:
和上一题一模一样,见上一题的题解吧!
程序代码:
#include<iostream>
#include<cstdio>
using namespace std;
const