• PAT 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 (≤), 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​​ (>) is the number of hobbies, and [ 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


     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 vector<int> parent(1001), _count(1001), vis(1001, false);
     6 int find(int x){
     7   int y=x, z;
     8   while(x!=parent[x]) x=parent[x];
     9   while(y!=x){
    10     z=parent[y];
    11     parent[y]=x;
    12     y=z;
    13   }
    14   return x;
    15 }
    16  
    17 bool _union(int a, int b){
    18   int roota=find(a), rootb=find(b);
    19   if(roota==rootb) return false;
    20   else{
    21       if(_count[roota]>_count[rootb]) {
    22           parent[rootb]=roota;
    23           _count[roota] += _count[rootb];
    24       }else{
    25           parent[roota]=rootb;
    26           _count[rootb] += _count[roota];
    27       }
    28     return true;
    29   }
    30 }
    31 //并查集解决这道题 很简单 用一个数组来记录根节点 用一个数组来记录每棵树的的人数  
    32 //这一题的测试数据比较少  不压缩路径也没有关系
    33 int main(){
    34   int n, i;
    35   scanf("%d", &n);
    36   fill(_count.begin(), _count.end(), 0);
    37   for(i=1; i<=1000; i++) parent[i]=i;//开始条件为i<1000有四个测试点不能通过  修改成i<=1000就能全部通过了
    38   for(i=0; i<n; i++){
    39     int cnt, j, temp, hobby;
    40     scanf("%d: %d", &cnt, &temp);
    41     _count[find(temp)]++;
    42     vis[temp]=true;
    43     for(j=1; j<cnt; j++){
    44       scanf("%d", &hobby);
    45       _union(temp, hobby);
    46       temp=hobby;
    47       vis[temp]=true;
    48     }
    49   }
    50   vector<int> ans;
    51   for(i=1; i<=1000; i++)
    52     if(parent[i]==i && vis[i]) ans.push_back(_count[i]);
    53   sort(ans.begin(), ans.end());
    54   printf("%d
    ", ans.size());
    55   printf("%d", ans[ans.size()-1]);
    56   for(i=ans.size()-2; i>=0; i--) printf(" %d", ans[i]); 
    57   return 0;
    58 }
  • 相关阅读:
    Asp.net mvc项目分页功能
    asp.net mvc邮箱激活
    4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
    3. Spring 核心之 IOC(控制反转) & DI(依赖注入)
    2. Spring 的 HelloWorld
    1. Spring 简介以及关于 Eclipse 的 Spring Tool Suite 插件安装
    早睡早起身体好,早睡早起有力量,晚安~~
    睡前写几句,缓解一下刷题的心情。。。
    ACM 杭电HDU 2084 数塔 [解题报告]
    ACM HDU 2041--超级楼梯题解
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9585542.html
Copyright © 2020-2023  润新知