本文主要是介绍cf Educational Codeforces Round 65 C. News Distribution,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
C. News Distribution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
The first line contains two integers n and m (1≤n,m≤5⋅10^5) — the number of users and the number of groups of friends, respectively.
Then m lines follow, each describing a group of friends. The i-th line begins with integer ki (0≤ki≤n) — the number of users in the i-th group. Then ki distinct integers follow, denoting the users belonging to the i-th group.
It is guaranteed that ∑i=1mki≤5⋅10^5
.
Output
Print n
integers. The i-th integer should be equal to the number of users that will know the news if user i
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
中文:
有n个人,每个人属于一些社交圈,一个社交圈的里的人会传播消息。现在给处社交圈,问你每个人作为消息传播源的情况下,对应知道消息的人数。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef pair<int,int> pii;
const int maxn = 500001;
int father[maxn];
int Rank[maxn];
int n,m;
void init()
{for(int i=1;i<=n;i++){father[i]=i;Rank[i]=1;}
}int Find(int x)
{if(x!=father[x])return father[x] = Find(father[x]);elsereturn x;
}void unite(int x,int y)
{x = Find(x);y = Find(y);if(x==y)return;if(Rank[x]<Rank[y]){swap(x,y);}father[y]=x;Rank[x]+=Rank[y];
}int main()
{ios::sync_with_stdio(false);while(cin>>n>>m){init();int c;for(int i=1;i<=m;i++){cin>>c;int mark;if(c){int val;cin>>mark;for(int j=1;j<c;j++){cin>>val;unite(mark,val);}}}for(int i=1;i<=n;i++){int x = Find(i);if(i!=n)cout<<Rank[x]<<" ";elsecout<<Rank[x]<<endl;}}return 0;
}
解答:
裸的并查集,把按秩合并的部分改成人数就完事了
这篇关于cf Educational Codeforces Round 65 C. News Distribution的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!