一、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 int N=50000+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 n,m;
int t=1;
while(~scanf("%d %d",&n,&m)){
if(n==0 && m==0) break;
for(int i=1;i<=n;i++){
s[i]=i;
}
int x,y;
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("Case %d: %d\n",t++,cnt);
}
return 0;
}
3.The Suspects
题意:
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
输入:
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
输出:
For each case, output the number of suspects in one line.
样例输入:
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
样例输出:
4
1
1
解题思路:
我觉得这题是上两题的升级版,就是要统计和0接触过的人数。和之前一样,最后统计一下就行了。
程序代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int N=30000+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 gf(int x){
if(x==s[x])
return x;
int a=gf(s[x]);
s[x]=a;
return a;
}
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
if(n==0 && m==0) break;
for(int i=0;i<=n-1;i++){
s[i]=i;
}
while(m--){
int t;
int x,y;
scanf("%d",&t);
scanf("%d",&x);
for(int i=2;i<=t;i++){
scanf("%d",&y);
s[gf(y)]=gf(x);
// cin>>x;
// if(i==1){
// y=x;
// }else{
// union_tree(y,x);
// y=x;
// }
}
}
// for(int i=0;i<n;i++){
// cout<<i<<" "<<s[i]<<endl;
// }
int cnt=0;
for(int i=0;i<=n-1;i++){
if(s[gf(i)]==s[0])
cnt++;
// if(s[i]==s[0])
// cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
4.Find them, Catch them
题意:
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
-
D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs. -
A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
输入:
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.
输出:
For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”
样例输入:
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
样例输出:
Not sure yet.
In different gangs.
In the same gang.
解题思路:
这题的大意就是,有两个帮派,D之后的两个人就是不同帮派的人,A之后的两个人就是你需要判断是不是一个帮派。我觉得用数组写也可以啊,但是交了好几遍都是WA,气死我了,我觉得无敌正确,最后在网上查到了一个别人用并查集写的代码,就AC了,哼!
程序代码:(WA)气死我了!!!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e5+5;
int ans[N];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(ans,0,sizeof(ans));
int n,m;
scanf("%d %d",&n,&m);
while(m--){
char a;
int x,y;
getchar();
a=getchar();
scanf("%d %d",&x,&y);
if(a=='D'){
if(ans[x]==0&&ans[y]==0){
ans[x]=1;
ans[y]=-1;
}else if(ans[x]==0&&ans[y]!=0){
ans[x]=-ans[y];
}else if(ans[x]!=0&&ans[y]==0){
ans[y]=-ans[x];
}else if(ans[x]!=0&&ans[y]!=0&&ans[x]==ans[y]){
ans[x]=-ans[y];
}
}else if(a=='A'){
if(ans[x]==0||ans[y]==0){
printf("Not sure yet.\n");
}else if(ans[x]!=ans[y]){
printf("In different gangs.\n");
}else{
printf("In the same gang.\n");
}
}
}
}
return 0;
}
程序代码:(AC)
#include<cstdio>
#include<iostream>
using namespace std;
int par[200005];
int find(int x){
if(par[x]==x) return x;
else
return par[x]=find(par[x]);
}
void unit(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
else
par[y]=x;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<2*n;i++)
par[i]=i;
char p;
int x,y;
while(m--){
getchar();
scanf("%c %d %d",&p,&x,&y);
if(p=='D'){
unit(x,y+n); unit(x+n,y);
}
if(p=='A'){
if(find(x)==find(y))
cout<<"In the same gang."<<endl;
else if(find(x)==find(y+n))
cout<<"In different gangs."<<endl;
else
cout<<"Not sure yet."<<endl;
}
}
}
}
二、AtCoder
1.Go to School
题意:
Takahashi is a teacher responsible for a class of N students.
The students are given distinct student numbers from 1 to N.
Today, all the students entered the classroom at different times.
According to Takahashi’s record, there were Ai students in the classroom when student number i entered the classroom (including student number i).
From these records, reconstruct the order in which the students entered the classroom.
输入:
Input is given from Standard Input in the following format:
N
A1 A2 … AN
1≤N≤10^5
1≤Ai≤N
输出:
Print the student numbers of the students in the order the students entered the classroom.
样例输入:
3
2 3 1
样例输出:
3 1 2
解释:
First, student number 3 entered the classroom.
Then, student number 1 entered the classroom.
Finally, student number 2 entered the classroom.
解题思路:
题意就是输入学生i进入教室时教师的人数,输出他们进入教室的顺序。其实规律很简单,他们进入教室时有几个人,他们就是第几个进去的人,所以开一个数组记录一下就好了。
程序代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],ans[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ans[a[i]]=i;
}
for(int i=1;i<=n;i++){
if(i==1)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
return 0;
}
这篇博客介绍了如何运用并查集解决Vjudge、AtCoder等竞赛平台上的多个题目,包括朋友分桌问题、宗教信仰统计、嫌疑人隔离和帮派成员判断等。通过实例解析并查集的应用,帮助读者理解并解决这类问题。
353

被折叠的 条评论
为什么被折叠?



