zoukankan      html  css  js  c++  java
  • Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板

    题目:

    In some social network, there are nn users communicating with each other in mm groups of friends. Let's analyze the process of distributing some news between users.

    Initially, some user xx receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.

    For each user xx you have to determine what is the number of users that will know the news if initially only user xx starts distributing it.

    输入:

    The first line contains two integers nn and mm (1n,m51051≤n,m≤5⋅105) — the number of users and the number of groups of friends, respectively.

    Then mm lines follow, each describing a group of friends. The ii-th line begins with integer kiki (0kin0≤ki≤n) — the number of users in the ii-th group. Then kiki distinctintegers follow, denoting the users belonging to the ii-th group.

    It is guaranteed that i=1mki5105∑i=1mki≤5⋅105.

    输出:

    Print nn integers. The ii-th integer should be equal to the number of users that will know the news if user ii starts distributing it.

    样例:

    Example

    Input
    7 5
    3 2 5 4
    0
    2 1 2
    1 1
    2 6 7
    
    Output
    4 4 1 4 4 2 2 

    分析:不想分析。。。

     1 #include<iostream>
     2 #include<sstream>
     3 #include<cstdio>
     4 #include<cstdlib>
     5 #include<string>
     6 #include<cstring>
     7 #include<algorithm>
     8 #include<functional>
     9 #include<iomanip>
    10 #include<numeric>
    11 #include<cmath>
    12 #include<queue>
    13 #include<vector>
    14 #include<set>
    15 #include<cctype>
    16 const double PI = acos(-1.0);
    17 const int INF = 0x3f3f3f3f;
    18 const int NINF = -INF - 1;
    19 typedef long long ll;
    20 #define MOD 1000007
    21 using namespace std;
    22 int far[500005];
    23 int sum[500005];
    24 int n, m;
    25 int find(int x)
    26 {
    27     if(far[x] == x) return x;
    28     else return far[x] = find(far[x]);
    29 }
    30 bool check(int x, int y)
    31 {
    32     return find(x) == find(y);
    33 }
    34 void unite(int x, int y)
    35 {
    36     x = find(x), y = find(y);
    37     if(x == y) return;
    38     far[y] = x;
    39     sum[x] += sum[y];
    40 }
    41 void init(int n)
    42 {
    43     for(int i = 0;i <= n;i++)
    44     {
    45         far[i] = i;
    46         sum[i] = 1;
    47     }
    48 }
    49 int main()
    50 {
    51     cin >> n >> m;
    52     init(n);
    53     int t;
    54     int a, b;
    55     while (m--)
    56     {
    57         cin >> t;
    58         if (!t) continue;
    59         cin >> a;
    60         for (int i = 1; i < t; ++i)
    61         {
    62             cin >> b;
    63             if (!check(a, b) )
    64             {
    65                 unite(a, b);
    66             }
    67         }
    68     }
    69     for(int i = 1 ;i <= n; ++i)
    70     {
    71         int x = find(i);
    72         cout << sum[x] << ' ';
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    oc对象的内存管理
    OC类的本质,深入探讨,load方法和initialize方法
    OC特有语法:分类category,给NSString增加方法计算字符串中数字的个数
    OC的类的构造方法
    OC的@property 和 @synthesize id
    HTML5 Canvas时间效果
    各浏览器的Hack写法【转】
    你自认为理解了JavaScript?【转】
    八大疯狂的HTML5 Canvas及WebGL动画效果——8 CRAZY ANIMATIONS WITH WEBGL AND HTML5 CANVAS【收藏】
    一些达成共识的JavaScript编码风格约定【转】
  • 原文地址:https://www.cnblogs.com/veasky/p/11223148.html
Copyright © 2011-2022 走看看