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

结尾无空行

思路

想用表示兴趣的整数作为索引,建立亲戚树,爱好有交集的人构成一棵树,共享一个父亲(如题和树中一个人的爱好有交集就好),祖宗的父亲指向自己,并且祖宗负责记录树里一共有多少个节点。

通过代码

#include<bits/stdc++.h>
using namespace std;
int many[1002],father[1001];
bool cmp(int a,int b){
    return a>b;
}
int findfather(int a){
    if(father[a]==0){
        father[a]=a;
    }else if(father[a] != a){
        father[a]=findfather(father[a]); 
    }
    return father[a];
}
void unid(int a,int b){
    int ancestor,bfather;
    ancestor=findfather(a);
    if(father[b]==ancestor)return;//*
    if(father[b]!=0){
        bfather=findfather(b);
        father[b]!=ancestor;
        b=bfather;
    }
    many[ancestor]+=many[b];
    many[b]=0;
    father[b]=ancestor;
    //printf("现在兴趣%d和兴趣%d都在%d俱乐部(%d人)\n",a,b,ancestor,many[ancestor]);
}
int main(){
    int n,pern,thisfather,total=0;
    memset(father,0,sizeof(father));
    memset(many,0,sizeof(many));
    vector <int> person(1001);
    person[0]=0;
    scanf("%d",&n);
    for(int j=0;j<n;j++){
        scanf("%d",&pern);
        getchar();
        for(int i=0;i<pern;i++){
            scanf("%d",&person[i]);
        }
        for(int i=0;i<pern-1;i++){
            unid(person[i],person[i+1]);
        }
        thisfather=findfather(person[0]);
        many[thisfather]+=1;
        //printf("处理完%d号嘉宾,%d俱乐部(%d人)\n\n",j,thisfather,many[thisfather]);
    }
    sort(many+1,many+1002,cmp);
    for(int i=1;i!=1002 && many[i]!=0;i++){
        total++;
    }
    printf("%d\n",total);
    for(int i=1;i!=1002 && many[i]!=0;i++){
        printf("%d",many[i]);
        if(i!=total)printf(" ");
    }
    return 0;
}

总结

知识

方法

  1. 一开始没有将儿子节点存储的数量置为0(many[儿子兴趣]!=0),而最后又用many数组来输出非0值,以后在输出数组非0值时注意确认是否将不需要的项都置为0了。
  2. 一开始整合a和b的父亲节点的时候没有考虑到b可能有父亲,直接找到a的父亲(a都知道找父亲咋b不知道,b:那我走?)然后赋给father[b],以后但凡是处理两项,检查一下a项考虑到的点是否也都同样为b项考虑到了。
  3. 一开始没有考虑到b和a本来就是一个爸爸的情况,想得挺美,把b的家族加到a的族谱里,然后把b家的人数加到a家的人数里,再把b家的族谱清空,完美!但是如果b本来就和a是一家人,我加我自己?还要我杀我自己?以后处理两项的时候一定不要忘了这两项可能相等的情况。
  4. 一开始在findfather里没写这一句和相关的逻辑
    father[a]=findfather(father[a]); 
    当时想的是递归没必要啊,在给每个兴趣设置father的时候都是找到父亲兴趣或者爷爷兴趣,然后把最长辈的那一辈作为父亲,所以每个节点在设置的时候就都是直接接在祖宗节点上的。因此找父亲的时候,father[a]要么是自己,要么是他爹,干啥还要递归呢?我忽略了一种情况就是:父亲可能是先认了儿子再找到自己的爸爸的,这种情况下儿子再找祖宗就需要递归,比如儿子认自己的儿子的时候就得递归找到爷爷,再把曾孙的数量告诉他。并查集的焦点之一就在这呀,节点递归找到祖宗后将祖宗作为自己的父亲,这样就可以避免某些情况下递归次数过多导致运行超时。
  5. 最离谱的错误,有个函数叫findfather(int a),有个数组叫father,慢慢就写成findfather[int a],报的错长这样

    invalid conversion from ‘int (*)(int)’ to ‘int’ [-fpermissive]

    下次注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值