A1107 Social Clusters (30分)

一、技术总结

  1. 这是一道并查集的题目,这里无非就是明白题目的含义,然后将套路用上基本上问题不大。
  2. 一个是father数组用于存储父亲结点,isRoot用于存储根结点,如果题目需要,还可以建立一个course数组,用于存储,例如这个题目就是用于判断该该兴趣是否有人涉及。
  3. findFather函数,用于查找结点的父亲结点,同时包含压缩路径。
  4. Union函数,用于合并。
  5. 初始化函数。

二、参考代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int father[maxn];
int isRoot[maxn] = {0};
int course[maxn] = {0};
int findFather(int x){
    int a = x;
    while(x != father[x]){
        x = father[x];
    }
    //路径压缩
    while(a != father[a]){
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}
void Union(int a, int b){
    int faA = findFather(a);
    int faB = findFather(b);
    if(faA != faB){
        father[faA] = faB;
    }
}
void init(int n){
    for(int i = 1; i <= n; i++){
        father[i] = i;
        //isRoot[i] = false;
    }
}
bool cmp(int a, int b){//将isRoot数组从大到小排序
    return a > b;
}
int main(){
    int n, k, h;
    scanf("%d", &n);
    init(n);
    for(int i = 1; i <= n; i++){
        scanf("%d:", &k);
        for(int j = 0; j < k; j++){
            scanf("%d", &h);
            if(course[h] == 0){
                course[h] = i;
            }
            Union(i, findFather(course[h]));
        }
    }
    for(int i = 1; i <= n; i++){
        isRoot[findFather(i)]++;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(isRoot[i] != 0){
            ans++;
        }
    }
    printf("%d\n", ans);
    sort(isRoot+1, isRoot+n+1, cmp);
    for(int i = 1; i <= ans; i++){
        if(i != 1) printf(" ");
        printf("%d", isRoot[i]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tsruixi/p/12350051.html

时间: 2024-11-08 06:28:24

A1107 Social Clusters (30分)的相关文章

PAT-1107 Social Clusters (30 分) 并查集模板

1107 Social Clusters (30 分) 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. Y

并查集——A1107.Social Clusters(30)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int N = 1010; int father[N]; int isRoot[N] = {0}; int course[N] = {0}; int findFather(int x){ int a = x; while(x != father[

1107. Social Clusters (30)

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 fi

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 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

PAT_A1107#Social Clusters

Source: PAT A1107 Social Clusters (30 分) Description: 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

pta08-图7 公路村村通 (30分)

08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N):随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到N编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路. 输入样例: 6

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of lan

5-10 公路村村通 (30分)

5-10 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道路数目MM(\le 3N≤3N):随后的MM行对应MM条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本.为简单起见,城镇从1到NN编号. 输出格式: 输出村村通需要的最低成本.如果输入数据不足以保证畅通,则输出-1?1,

pta5-9 Huffman Codes (30分)

5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem o