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⋅105) — 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⋅105.
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
思路:
赤裸裸的并查集问题,只需让爸爸存储自己有几个二子就行,然后输出自己爸爸存储的数中有几个
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<sstream>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
int fish[5000040],number[5000040];
int head(int p){
if(fish[p]!=p)fish[p]=head(fish[p]);
return fish[p];
}
void cmb(int a,int b){
int la=head(a),lb=head(b);
if(la!=lb){
fish[lb]=la;number[la]+=number[lb];
}
}
int main(){
int n,m,i,x,y,z;
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=0;i<=n;i++){
fish[i]=i;number[i]=1;
}
while(m--){
cin>>x;
if(x==0)continue;
cin>>y;
for(i=1;i<x;i++){
cin>>z;
cmb(y,z);
y=z;
}
}
for(i=1;i<=n;i++){
if(i==n){
cout<<number[head(i)];break;
}
cout<<number[head(i)]<<" ";
}
cout<<endl;
}