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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1010;
int isroot[maxn]={0};
int father[maxn];
int course[maxn]={0};
bool cmp(int a,int b){
return a>b;
}
int findfather(int x){
int t=x;
while(father[t]!=t){
t=father[t];
}
while(father[x]!=x){//可以不压缩路径,但是时复会增大
int m=father[x];
father[x]=t;
x=m;
}
return t;
}
void Union(int a,int b){
int fa=findfather(a),fb=findfather(b);
if(fa!=fb){
father[fa]=fb;
}
}
int main(){
freopen("in.txt","r",stdin);
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++){
father[i]=i;
}
for(i=1;i<=n;i++){
int x,m;
scanf("%d:",&m);
for(int j=0;j<m;j++){
scanf("%d",&x);
if(course[x]==0){//如果本科程没有人选,那么课程为i所有
course[x]=i;
}
Union(i,course[x]);//将i与第一个选择课程x的人合并
}
}
int cnt=0;
for(i=1;i<=n;i++){
int t=findfather(i);
isroot[t]++;
if(t==i){
cnt++;
}
}
sort(isroot+1,isroot+n+1,cmp);
printf("%d\n%d",cnt,isroot[1]);
for( i=2;i<=cnt;i++){
printf(" %d",isroot[i]);
}
return 0;
}
暴力解法
#include<iostream>
#include<set>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
set <int> cluster[1010];
int num[1010]={0},n,cnum=0;
vector<int> h[1010];
bool cmp(int a,int b){
return a>b;
}
bool samed[1010]={false};
queue<int> findcluser(int h[],int m){
queue <int>q;
for(int k=0;k<cnum;k++){
for(int j=0;j<m;j++){
if(cluster[k].find(h[j])!=cluster[k].end()){
q.push(k);
break;
}
}
}
return q;
}
int main(){
freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++){
int m,t;
scanf("%d:",&m);
for(int j=0;j<m;j++){
scanf("%d",&t);
h[i].push_back(t);
}
}
for(int i=0;i<n;i++){
if(!samed[i]){
num[cnum]=1;
set<int> cluster;
for(int k=0;k<h[i].size();k++){
cluster.insert(h[i][k]);
}
samed[i]=true;
bool flag=true;
while(flag){
flag=false;
for(int j=i+1;j<n;j++){
if(!samed[j]){
for(int k=0;k<h[j].size();k++){
if(cluster.find(h[j][k])!=cluster.end()){
flag=true;
samed[j]=true;
break;
}
}
if(flag){
num[cnum]++;
for(int k=0;k<h[j].size();k++){
cluster.insert(h[j][k]);
}
break;
}
}
}
}
cnum++;
}
}
sort(num,num+cnum,cmp);
printf("%d\n%d",cnum,num[0]);
for(int i=1;i<cnum;i++)
printf(" %d",num[i]);
return 0;
}