• Shaking Your Cellphone(并查集)


    Shaking Your Cellphone

    Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu

    Description

    Do you know a software called "WeiXin", a kind of chatting tool? There is an application called "Shaking for a while". Once you shake your cellphone, you can find those who shake their cellphone at same time.
    One day when Tom was shaking his cellphone, he thought about a problem: how large the friends group would be if everyone made friends once they find others who shake cellphone at same time and combine their friends group?
    We assume all the people are lonely and have no friends at the beginning. In other word, their friends group only contains themselves.

    Input

    First line contains an integer T (0 < T <= 10), indicate there are T cases.
    For each case, the first line contains an integer N (0 < N <= 1000), indicate the number of people who would shake their cellphone.
    Then follows N lines. Every line comes with an integer K[i] (0 <= K[i] <= 1440), indicate the i-th person shakes cellphone K times a day. Then follows K[i] strings shown as "HH:MM", indicate the time the i-th person shakes cellphone.

    Output

    For each test case, the first line contains an integer X indicate how many friends group amount the people; the second line comes X integers in nondescending order, indicate the scale of each friends group.

    Sample Input

    1
    4
    3 00:00 00:01 00:02
    2 00:01 00:03
    1 00:03
    1 00:04

    Sample Output

    2
    1 3

    知识点:STL map和并查集

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <set>
     5 #include <map>
     6 #include <vector>
     7 #include <queue>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <cmath>
    11 using namespace std;
    12 
    13 map<string, int> M;
    14 string s;
    15 int t, n, k;
    16 int f[1005];
    17 int cnt[1005];
    18 
    19 int Find(int x)
    20 {
    21     if(x != f[x]) return f[x] = Find(f[x]);
    22     return x;
    23 }
    24 
    25 int main()
    26 {
    27     int i;  //index
    28     scanf("%d", &t);
    29     while(t--)
    30     {
    31         M.clear();
    32         scanf("%d", &n);
    33         for(i = 1; i <= n; i++) f[i] = i;  //初始化父亲集合
    34         for(i = 1; i <= n; i++)
    35         {
    36             scanf("%d", &k);
    37             while(k--)
    38             {
    39                 cin >> s;
    40                 int x = M[s];
    41                 if(!x) M[s] = i;
    42                 else
    43                 {
    44                     int fx = Find(x);
    45                     int fy = Find(i);
    46                     if(fx > fy) f[fx] = fy;
    47                     else if(fx < fy) f[fy] = fx;
    48                 }
    49             }
    50         }
    51         memset(cnt, 0, sizeof(cnt));  //cnt[i]记录以i为父亲结点的结点数
    52         int sum = 0;
    53         for(i = 1; i <= n; i++)
    54         {
    55             int fi = Find(i);
    56             if(!cnt[fi]) sum++;
    57             cnt[fi]++;
    58         }
    59         sort(cnt + 1, cnt + n + 1);
    60         printf("%d\n", sum);
    61         for(i = 1; !cnt[i]; i++){}
    62         printf("%d", cnt[i]);
    63         for(++i; i <= n; i++)
    64             printf(" %d", cnt[i]);
    65         printf("\n");
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    pgsql 记录
    tomcat下放两个spring boot项目
    nigex 反向代理
    tomcat7里放springboot
    postgresql 建表语句
    从最大似然到EM算法浅解(转载)
    深度学习资料
    【vuejs小项目——vuejs2.0版本】组件化的开发方式
    【vuejs小项目——vuejs2.0版本】单页面搭建
    如何关闭eslint
  • 原文地址:https://www.cnblogs.com/cszlg/p/2966432.html
Copyright © 2020-2023  润新知