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;
    }
    
  • 相关阅读:
    VS远程调试亲历
    IIS7.5 配置虚拟目录的经历
    IE 兼容一问题一小记
    寻找 IBatisNet 批量插入(批量复制) 的心路历程
    Linq 多连接及 left join 实例 记录
    easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下
    HDU1754
    HDU1166
    线段树模板
    HDU1599(Floyd最小环)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11361204.html
Copyright © 2011-2022 走看看