-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_News_Distribution.cpp
114 lines (109 loc) · 2.45 KB
/
C_News_Distribution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define loop(o, n) for (int i = 0; i < n; i++)
#define py cout << "YES" << endl
#define pn cout << "NO" << endl
using namespace std;
// #define m 1e9+7
class DisjointSet
{
public:
vector<int> rank, parent, size;
DisjointSet(int n)
{
rank.resize(n + 1, 0);
parent.resize(n + 1);
size.resize(n + 1);
for (int i = 0; i < n; i++)
{
parent[i] = i;
size[i] = 1;
}
}
int findUParent(int node)
{
if (node == parent[node])
return node;
return parent[node] = findUParent(parent[node]);
}
void UnionByRank(int u, int v)
{
int ulp_u = findUParent(u);
int ulp_v = findUParent(v);
if (ulp_u == ulp_v)
return;
if (rank[ulp_u] < rank[ulp_v])
{
parent[ulp_u] = ulp_v;
}
else if (rank[ulp_v] < rank[ulp_u])
{
parent[ulp_v] = ulp_u;
}
else
{
parent[ulp_v] = ulp_u;
rank[ulp_u]++;
}
}
void UnionBySize(int u, int v)
{
int ulp_u = findUParent(u);
int ulp_v = findUParent(v);
if (ulp_u == ulp_v)
return;
if (size[ulp_u] < size[ulp_v])
{
parent[ulp_u] = ulp_v;
size[ulp_v] += size[ulp_u];
}
else
{
parent[ulp_v] = ulp_u;
size[ulp_u] += size[ulp_v];
}
}
};
int main()
{
int t = 1;
// cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
DisjointSet dsu(n + 1);
while (m--)
{
int k;
cin >> k;
int x;
if (k)
{
cin >> x;
}
for (int i = 1; i < k; i++)
{
int y;
cin >> y;
dsu.UnionByRank(x, y);
}
}
map<int, int> mp;
for (int i = 1; i <= n; i++)
mp[dsu.findUParent(i)]++;
for (int i = 1; i <= n; i++)
cout << mp[dsu.findUParent(i)] << " ";
cout << endl;
}
return 0;
}