PAT 1107 Social Clusters

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:

Ki​: hi​[1] hi​[2] ... hi​[Ki​]

where Ki​ (>0) is the number of hobbies, and hi​[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

总结: 每个人有不同的爱好,需要找到共同爱好的人组成一个社团

代码:这里发现未对map的键进行赋值,但是对其进行了访问,最后用 auto 循环的时候会打印这种情况

#include 
#include 
#include 
#include 
using namespace std;

vector> v(1010);
vector b;//记录每个社团有多少个人
int n;
bool cmp(int a,int b){return a>b;}

int main(){
    scanf("%d",&n);
    b.resize(n);
    
    int t,w,index=0,cot=0;
    for(int i=0;i a(t),q;//q表示成共加入的社团
        bool sign=false;
        for(int j=0;jfirst,it->second);
        cout << endl;   //这里为什么为赋值的map也打印出来了呢?因为24行处我们对这些键进行了访问,等于间接去赋值了
    }*/
    sort(b.begin(),b.end(),cmp);
    printf("%d\n",index);
    for(int i=0;i

柳神代码:

#include 
#include 
#include 
using namespace std;
vector father, isRoot;
int cmp1(int a, int b){
    return a > b;
}
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;//因为再上一个循环中计算出来了跟节点,而x存着根节点的值,这里将路径上的每一个点都指向了根节点 
    } 
    //while(x!=father[x]) x=findFather(father[x]);//这是递归查找父节点,也实现了路径压缩 
    return x;
}
void Union(int a, int b) {
    int faA = findFather(a);
    int faB = findFather(b);
    if(faA != faB)//如果两个人不在同一个集合中,那么就加入这个集合 
        father[faA] = faB;
}
int main() {
    int n, k, t, cnt = 0;
    int course[1001] = {0};
    scanf("%d", &n);
    father.resize(n + 1);
    isRoot.resize(n + 1);
    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", &t);
            //判断当前这个爱好是否有人, 如果之前有人有这个爱好,那么会存储着
			//第一个人的id,通过并查集找到这个人,并将他加入这个集合当中 
            if(course[t] == 0)
                course[t] = i;
            Union(i, findFather(course[t]));
        }
    }
    for(int i = 1; i <= n; i++)
        isRoot[findFather(i)]++;//计算每个并查集里面的人数 
    for(int i = 1; i <= n; i++) {
        if(isRoot[i] != 0)//统计并查集的个数 
            cnt++;
    }
    printf("%d\n", cnt);
    sort(isRoot.begin(), isRoot.end(), cmp1);
    for(int i = 0; i < cnt; i++) {
        printf("%d", isRoot[i]);
        if(i != cnt - 1) printf(" ");
    }
    return 0;
}

好好学习,天天向上!

我要考研!

你可能感兴趣的:(pat甲级,c++)