首页 > 其他 > 详细

CodeForces-1167C

时间:2019-08-16 01:15:58      阅读:112      评论:0      收藏:0      [点我收藏+]

链接:

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;
}

CodeForces-1167C

原文:https://www.cnblogs.com/YDDDD/p/11361204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!