Codeforces 1167c - News Distribution

Description

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.

Input

The first line contains two integers nn and mm (1≤n,m≤5⋅1051≤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 (0≤ki≤n0≤ki≤n) — the number of users in the ii-th group. Then kiki distinct integers follow, denoting the users belonging to the ii-th group.

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

Output

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.

Sample Input

Input

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

Output

4 4 1 4 4 2 2 

题意:

有n个使用者,m个组,每一组中有k个使用者

现在传递信息是这样的过程:第i个使用者需要传递给他的朋友,他的朋友再次传递给他的朋友,依次继续,直到没有朋友可以传递信息为止

求第i个使用者传递的信息可以被多少人知道

思路:

并查集的思想

在求第i个使用者传递的信息可以被多少人接收时:

也就是求与第i个使用者同父亲节点的个数,这里不能使用两层循环去查找(数据量太大)

可以事先打表,将 find(i) 出现的次数记录下来,直接输出 num[find(i)]

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 5e5+10;
int pre[MAXX];
int num[MAXX];

int init(int n)
{
    for(int i=1; i<=n; i++)
        pre[i] = i;
}
int find(int x)
{
    if(x != pre[x])
        pre[x] = find(pre[x]);
    return pre[x];
}

void UNION(int a,int b)
{
    int root1 = find(a);
    int root2 = find(b);
    if(root1 != root2)
        pre[root1] = root2;
}

int main()
{
    int n,m,k,x,y;
    cin >> n >> m;
    init(n);
    for(int i=0; i<m; i++){
        cin >> k;
        if(k == 0)
            continue;
        cin >> x;
        for(int j=1; j<k; j++){
            cin >> y;
            UNION(x,y);
            x = y;
        }
    }

    memset(num,0);
    for(int i=1; i<=n; i++){
        x = find(i);
        num[x]++;
    }
    for(int i=1; i<=n; i++){
        x = find(i);
        if(i != n)
            cout << num[x] << ' ';
        else
            cout << num[x] << endl;
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值