zoukankan      html  css  js  c++  java
  • CodeForces-1167C

    链接:

    https://vjudge.net/problem/CodeForces-1167C

    题意:

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

    Initially, some user x 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 x you have to determine what is the number of users that will know the news if initially only user x starts distributing it.

    思路:

    并查集, 在统计一个组的人数.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 5e5+10;
    int Fa[MAXN], Sum[MAXN];
    int n, m;
    
    int GetF(int x)
    {
        if (Fa[x] == x)
            return x;
        Fa[x] = GetF(Fa[x]);
        return Fa[x];
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> m;
        for (int i = 1;i <= n;i++)
            Fa[i] = i;
        for (int i = 1;i <= m;i++)
        {
            int k, h, v;
            cin >> k;
            if (k)
                cin >> h;
            int he = GetF(h);
            for (int j = 1;j < k;j++)
            {
                cin >> v;
                int tv = GetF(v);
                if (tv != he)
                    Fa[tv] = he;
            }
        }
        for (int i = 1;i <= n;i++)
        {
            int t = GetF(i);
            Sum[t]++;
        }
        for (int i = 1;i <= n;i++)
            cout << Sum[GetF(i)] << ' ';
        cout << endl;
    
        return 0;
    }
    
  • 相关阅读:
    java-date和Calendar运用
    java-date类使用练习(1)
    java-正则表达式
    JAVA-String类的应用
    java-重写equals
    java-抽象类、接口等实际应用 (酒店小案例)
    fuzz系列之libfuzzer
    ZAB协议(转)
    Paxos(转自wiki)
    Zookeeper简介和安装使用
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11361204.html
Copyright © 2011-2022 走看看