【1107】Social Clusters (30 分)

简介: 【1107】Social Clusters (30 分)【1107】Social Clusters (30 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
//并查集求社交网络个数
//key:course[h]用以记录任意一个喜欢活动h的人的编号
//findFather(course[h])即该人所在社交网络的根结点
const int N=1010;
int father[N];  //存放父亲结点
int isRoot[N]={0};  //记录每个结点是否作为某个集合的根结点
int course[N]={0};
int findFather(int x){  //查找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){  //合并a和b所在的集合
  int faA=findFather(a);
  int faB=findFather(b);
  if(faA != faB ){
    father[faA]=faB;
  }
}
void init(int n){  //初始化father[i]为i,且flag[i]为false
  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);  //输入i号人喜欢的活动h
      if(course[h] == 0){  //如果活动h第一次有人喜欢
        course[h]=i; //令i喜欢活动h
      }
      Union(i,findFather(course[h]));  //合并
    }
  }
  for(int i=1;i<=n;i++){ 
    isRoot[findFather(i)]++; //i的根结点是findFather(i),人数加1
  }
  int ans=0;  //记录集合的数目
  for(int i=1;i<=n;i++){
    if(isRoot[i] != 0){
      ans++;  //只统计isRoot[i]不为0的
    }
  }
  printf("%d\n",ans); //输出集合个数
  sort(isRoot+1,isRoot+n+1,cmp);  //从大到小排序,所有1都在前面
  for(int i=1;i<=ans;i++){  //依次输出每个集合的人数
    printf("%d",isRoot[i]);
    if(i < ans) printf(" ");
  }
  system("pause");
    return 0;   
}
相关文章
|
10月前
|
存储 C++
【PAT甲级 - C++题解】1107 Social Clusters
【PAT甲级 - C++题解】1107 Social Clusters
40 0
|
11月前
|
Oracle 关系型数据库 网络安全
笔记:2 Day + Real Application Clusters Guide
ndy database 远程awr Domain server cluster 对ASM 的增强,使ASM以服务的方式进行提供。
《J.P.Morgan's massive guide to machine learning and big data jobs in finance》电子版地址
J.P.Morgan's massive guide to machine learning and big data jobs in finance
72 0
《J.P.Morgan's massive guide to machine learning and big data jobs in finance》电子版地址
《Life-stage Prediction for Prod》电子版地址
Life-stage Prediction for Product Recommendation in E-commerce
46 0
《Life-stage Prediction for Prod》电子版地址
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
117 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
83 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
166 0
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
85 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
76 0
PAT (Advanced Level) Practice - 1044 Shopping in Mars(25 分)
PAT (Advanced Level) Practice - 1044 Shopping in Mars(25 分)
102 0

热门文章

最新文章