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