并查集合集(PAT甲级)

PAT 1118 Birds in Forest

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤10^​4​​ ) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B1 B2 ... B​K

​​where K is the number of birds in this picture, and B​i’s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 10​^4​​ .After the pictures there is a positive number Q (≤10 ^​4) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes if the two birds belong to the same tree, or No if not.

Sample Input:

4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7

Sample Output:

2 10
Yes
No

思路:将每张照片上的鸟使用Union合并,定义一个map完成鸟的编号到其数量的映射,方便计算集合的个数和鸟的总数。最后判断两只鸟是否属于一个集合只需判断findFather()是否相等即可。

AC代码:

#include <stdio.h>
#include <map>
using namespace std;
const int maxn=10010;
int n,k,a,first;
int father[maxn];

int findFather(int x){
    int a=x;
    while(x!=father[x])
        x=father[x];
    while(a!=father[a]){    //路径压缩
        int t=a;
        a=father[a];
        father[t]=x;
    }
    return x;
}

void Union(int a,int b){
    if(findFather(a)==findFather(b))
        return;
    father[findFather(a)]=findFather(b);
}

int main(){
    scanf("%d",&n);
    map<int,int> m;
    int cnt=0;
    for(int i=1;i<=maxn;i++)
        father[i]=i;
    for(int i=0;i<n;i++){
        scanf("%d%d",&k,&first);
        m[first]+=1;
        for(int j=0;j<k-1;j++){
            scanf("%d",&a);
            m[a]+=1;
            Union(first,a);
        }
    }
    for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
        if(findFather(it->first)==it->first)
            cnt++;
    }
    int q,q1,q2;
    scanf("%d",&q);
    printf("%d %d\n",cnt,m.size());
    for(int i=0;i<q;i++){
        scanf("%d%d",&q1,&q2);
        if(findFather(q1)==findFather(q2))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

PAT 1107 Social Clusters

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

Input Specification:
Each input file contains one test case. For each test case, the first line contains a positive integer N (≤1000), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

K​i: hi[1] hi[2] ... h​i[K​i]

where K​i(>0) is the number of hobbies, and h​i[j] is the index of the j-th hobby, which is an integer in [1, 1000].

Output Specification:
For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
3: 2 7 10
1: 4
2: 5 3
1: 4
1: 3
1: 4
4: 6 8 1 5
1: 4

Sample Output:

3
4 3 1

思路:使用multimap完成人到爱好的映射,将multimap中值相同的两个键进行Union(有相同爱好的两个人进行合并),再借助一个map记录集合的个数及每个集合的人数。

AC代码:

#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int father[1010];

int findFather(int x){
    int a=x;
    while(x!=father[x])
        x=father[x];
    while(a!=father[a]){
        int t=a;
        a=father[a];
        father[t]=x;
    }
    return x;
}

void Union(int x,int y){
    if(findFather(x)==findFather(y))
        return;
    father[findFather(x)]=findFather(y);
}

bool cmp(int a,int b){
    return a>b;
}

int main(){
    int n,k,h,cnt=0;
    multimap<int,int> m;
    map<int,int> m1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        father[i]=i;
    for(int i=1;i<=n;i++){
        scanf("%d:",&k);
        for(int j=0;j<k;j++){
            scanf("%d",&h);
            m.insert(make_pair(i,h));
        }
    }
    multimap<int,int>::iterator it1,it2;
    for(it1=m.begin();it1!=m.end();it1++){
        for(it2=m.begin();it2!=it1;it2++){
            if(it1->second==it2->second){
                Union(it1->first,it2->first);
            }
        }
    }
    for(int i=1;i<=n;i++){
        m1[findFather(i)]+=1;
    }
    printf("%d\n",m1.size());
    map<int,int>::iterator it;
    vector<int> ans;
    for(it=m1.begin();it!=m1.end();it++){
        ans.push_back(it->second);
    }
    sort(ans.begin(),ans.end(),cmp);
    printf("%d",ans[0]);
    for(int i=1;i<ans.size();i++)
        printf(" %d",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值