目录
并查集之详解
A - 并查集
Description
如题,现在有一个并查集,你需要完成合并和查询操作。
Input
第一行包含两个整数 N,M ,表示共有 N 个元素和 M 个操作。
接下来 M 行,每行包含三个整数 Zi,Xi,Yi 。
当 Zi=1 时,将 Xi 与 Yi 所在的集合合并。
当 Zi=2 时,输出 Xi 与 Yi 是否在同一集合内,是的输出Y
;否则输出 N
。
Output
对于每一个 Zi=2 的操作,都有一行输出,每行包含一个大写字母,为 Y
或者 N
。
Sample 1
Inputcopy | Outputcopy |
---|---|
4 7 2 1 2 1 1 2 2 1 2 1 3 4 2 1 4 1 2 3 2 1 4 | N Y N Y |
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5,M=2e5+5;
int n,m,x,y,f[N],z;
int find(int k){
if(f[k]==k) return k;
return f[k]=find(f[k]);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++){
cin>>z>>x>>y;
if(z==1){
f[find(x)]=find(y);
}
else{
if(find(x)==find(y)) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}
B - 亲戚
Background
若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
Description
规定:x 和 y 是亲戚,y 和 z 是亲戚,那么 x 和 z 也是亲戚。如果 x,y 是亲戚,那么 x 的亲戚都是 y 的亲戚,y 的亲戚也都是 x 的亲戚。
Input
第一行:三个整数 n,m,p,(n,m,p≤5000),分别表示有 n 个人,m 个亲戚关系,询问 p 对亲戚关系。
以下 m 行:每行两个数 Mi,Mj,1≤Mi, Mj≤N,表示 Mi 和 Mj 具有亲戚关系。
接下来 p 行:每行两个数Pi,Pj,询问 Pi 和 Pj 是否具有亲戚关系。
Output
p 行,每行一个 Yes
或 No
。表示第 i 个询问的答案为“具有”或“不具有”亲戚关系。
Sample 1
Inputcopy | Outputcopy |
---|---|
6 5 3 1 2 1 5 3 4 5 2 1 3 1 4 2 3 5 6 | Yes Yes No |
#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int n,m,p,x,y,f[N];
int find(int k){
if(f[k]==k) return k;
return f[k]=find(f[k]);
}
int main(){
cin>>n>>m>>p;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++){
cin>>x>>y;
f[find(x)]=find(y);
}
for(int i=1;i<=p;i++){
cin>>x>>y;
if(find(x)==find(y)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
C - 相亲
Background
小明在 A 公司工作,小红在 B 公司工作。
Description
这两个公司的员工有一个特点:一个公司的员工都是同性。
A 公司有 N 名员工,其中有 P 对朋友关系。B 公司有 M 名员工,其中有 Q 对朋友关系。朋友的朋友一定还是朋友。
每对朋友关系用两个整数 (Xi,Yi) 组成,表示朋友的编号分别为Xi,Yi。男人的编号是正数,女人的编号是负数。小明的编号是 1,小红的编号是 −1。
大家都知道,小明和小红是朋友,那么,请你写一个程序求出两公司之间,通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。
Input
输入的第一行,包含 4 个空格隔开的正整数 N,M,P,Q。
之后 P 行,每行两个正整数 Xi,Yi。
之后 Q 行,每行两个负整数 Xi,Yi。
Output
输出一行一个正整数,表示通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。
Sample 1
Inputcopy | Outputcopy |
---|---|
4 3 4 2 1 1 1 2 2 3 1 3 -1 -2 -3 -3 | 2 |
HINT
使用二维数组,就不用考虑女生组的负数。
#include<bits/stdc++.h>
using namespace std;
const int N=2e4+5;
int n,m,p,q,x,y,l1=1,l2=1;
int f[N][2];//开二维数组
int find(int s,int t){ //初始0 父亲一定是自己
if(f[s][t]==s||f[s][t]==0) return s;
return f[s][t]=find(f[s][t],t);
}
int main(){
cin>>n>>m>>p>>q;
for(int i=1;i<=p;i++){
cin>>x>>y;
int a=find(x,0),b=find(y,0);
if(a!=b){//合并
if(a>b)swap(a,b);//让小数为树根,之后就可直接判断1
f[b][0]=a;
}
}
for(int i=1;i<=q;i++){
cin>>x>>y;
int a=find(-x,1),b=find(-y,1);//注意转为正
if(a!=b){
if(a>b)swap(a,b);
f[b][1]=a;
}
}
for(int i=2;i<=max(n,m);i++){//从2开始计数
if(find(f[i][0],0)==1)l1++;
if(find(f[i][1],1)==1)l2++;
//前面我们已经将女生负编号转正,此时只需判断为1
}
cout<<min(l1,l2)<<endl;
return 0;
}
D - 修复公路
Background
A 地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车。政府派人修复这些公路。
Description
给出 A 地区的村庄数 N,和公路数 M,公路是双向的。并告诉你每条公路的连着哪两个村庄,并告诉你什么时候能修完这条公路。问最早什么时候任意两个村庄能够通车,即最早什么时候任意两条村庄都存在至少一条修复完成的道路(可以由多条公路连成一条道路)。
Input
第 1 行两个正整数 N,M。
下面 M 行,每行 33 个正整数 x,y,t,告诉你这条公路连着 x,y 两个村庄,在时间t时能修复完成这条公路。
Output
如果全部公路修复完毕仍然存在两个村庄无法通车,则输出 −1,否则输出最早什么时候任意两个村庄能够通车。
Sample 1
Inputcopy | Outputcopy |
---|---|
4 4 1 2 6 1 3 4 1 4 5 4 2 3 | 5 |
HINT
突破点在于要使所有村庄任意通行,必须做到村庄之间能环形相接, 而这些环形相接的路段数量满足==n。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,t,x,y;
int f[N];
struct node{
int x,y,t;
bool operator < (const node &y) const{
return t<y.t; //设定按照t由小到大排列,因为修路时间越快越好
}
}tt[N];
int find(int s){
if(f[s]==s||f[s]==0) return s;
return f[s]=find(f[s]);
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>tt[i].x>>tt[i].y>>tt[i].t;
}
sort(tt+1,tt+1+m);
for(int i=1;i<=m;i++){
int a=find(tt[i].x ),b=find(tt[i].y );
if(a!=b){
f[b]=a;n--;
if(n==1){
cout<<tt[i].t<<endl;
return 0;
}
}
}
cout<<"-1"<<endl;
return 0;
}
E - 一中校运会之百米跑
Background
在一大堆秀恩爱的 ** 之中,来不及秀恩爱的苏大学神踏着坚定(?)的步伐走向了 100100 米跑的起点。这时苏大学神发现,百米赛跑的参赛同学实在是太多了,连体育老师也忙不过来。这时体育老师发现了身为体育委员的苏大学神,便来找他帮忙。
可是苏大学神需要热身,不然跑到一半就会抽(筋)、于是他就找到了你。。。如果你帮助体育老师解决了问题,老师就会给你 55 个积分。
Description
假设一共有 N(2≤N≤2×104)个参赛选手。(尼玛全校学生都没这么多吧)
老师会告诉你这 N 个选手的名字。
接着会告诉你 M(11≤M≤106)句话,即告诉你学生 A 与学生 B 在同一个组里。
如果学生 A 与学生 B 在同一组里,学生 B 与学生 C 也在同一组里,就说明学生 A 与学生 C 在同一组。
然后老师会问你 K(1≤K≤106)句话,即学生 X 和学生 Y 是否在同一组里。
若是则输出 Yes.
,否则输出 No.
。
Input
第一行输入 N 和 M。
接下来 N 行输入每一个同学的名字。
再往下 M 行每行输入两个名字,且保证这两个名字都在上面的 N 行中出现过,表示这两个参赛选手在同一个组里。
再来输入 K。
接下来输入 K 个体育老师的询问。
Output
对于每一个体育老师的询问,输出 Yes.
或 No.
。
Sample 1
Inputcopy | Outputcopy |
---|---|
10 6 Jack Mike ASDA Michel brabrabra HeHe HeHE papapa HeY Obama Jack Obama HeHe HeHE brabrabra HeHe Obama ASDA papapa Obama Obama HeHE 3 Mike Obama HeHE Jack papapa brabrabra | No. Yes. Yes. |
HINT
要点在于变量类型的选择,比如此题应该选择string类型存储运动员名字,而不是char
#include<map>
#include<cstdio>
#include<iostream>
using namespace std;
int n,m,k;
string s1,s2;
map<string,string> f;
string find(string x){
if(f[x]==x) return x;
else return f[x]=find(f[x]);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;++i){
cin>>s1;//选手名字
f[s1]=s1;
}
for(int i=1;i<=m;++i){
cin>>s1>>s2;
string x1=find(s1),x2=find(s2);//合并
if(x1!=x2) f[x1]=x2;
}
cin>>k;//询问k次
for(int i=1;i<=k;++i){
cin>>s1>>s2;
string x1=find(s1),x2=find(s2);//查询
if(x1!=x2) cout<<"No."<<endl;
else cout<<"Yes."<<endl;
}
return 0;
}
F - 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.
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
Inputcopy | Outputcopy |
---|---|
2 5 3 1 2 2 3 4 5 5 1 2 5 | 2 4 |
HINT
集合数从n个随着集合合并递减,最后即所求
(利用这种思想会比集合合并完再求不相同树根集合快很多)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,cnt,m,t;
int fa[1005];
int find(int x) {
return (fa[x]==x?x:fa[x]=find(fa[x]));
}
void join(int x,int y) {
fa[find(x)]=find(y);
}
int main() {
cin>>t;
while (t--) {
cin>>n>>m;
int ans=n;
memset(fa,1,sizeof fa);// 清空
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1,x,y;i<=m;i++) {//连边
cin>>x>>y;
if (find(x) != find(y)){//集合不同则合并
ans --;//如果两个原本不同的集合合并,则相当于,总集合数减 1
join (x, y);
}
}
cout<<ans<<endl;
}
return 0;
}
G - 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.
Input
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.
Output
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.
Sample
Inputcopy | Outputcopy |
---|---|
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 <cstring>
#include <cstdio>
using namespace std;
int n,cnt,m;
int fa[500005];
int find(int x) {
return (fa[x]==x?x:fa[x]=find(fa[x]));
}
void join(int x,int y) {
fa[find(x)]=find(y);
}
int main() {
while (cin>>n>>m&&n+m) {
int ans=n;
memset(fa,1,sizeof fa);
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1,x,y;i<=m;i++) {
cin>>x>>y;
if (find(x) != find(y)){
ans --;
join (x, y);
}
}
cout<<"Case "<<++cnt<<": "<<ans<<endl;
}
return 0;
}
H - 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.
Input
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.
Output
For each case, output the number of suspects in one line.
Sample
Inputcopy | Outputcopy |
---|---|
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 |
题目大意
N个学生,M组数据,每组中先给出了k,代表之后的k个数在同一集合内,求0所在的集合的元素个数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,cnt,m,k;
int fa[500005];
int find(int x) {
return (fa[x]==x?x:fa[x]=find(fa[x]));
}
void join(int x,int y) {
fa[find(x)]=find(y);
}
int main() {
while (cin>>n>>m&&n+m) {
cnt=1;
memset(fa,1,sizeof fa);// 清空
for (int i=0;i<=n;i++) fa[i]=i;
for (int i=1,x,y;i<=m;i++) {
cin>>k>>x;
for(int i=2;i<=k;i++){
cin>>y;
join (x, y);
}
}
for (int i=1;i<=n;i++) if(find(i)==find(0))cnt++;
cout<<cnt<<endl;
}
return 0;
}