forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
77 lines (68 loc) · 1.44 KB
/
H.cpp
File metadata and controls
77 lines (68 loc) · 1.44 KB
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
#include <bits/stdc++.h>
using namespace std;
int n, B, d[10100], sz[10100], idProvince[10100], numProvince, hq[10100];
vector <int> a[10100], provinces;
void visit(int x, int par)
{
vector <int> curChild;
for (auto y : a[x])
{
if (y == par) continue;
visit(y, x);
if (sz[y])
{
curChild.push_back(y);
sz[x] += sz[y];
if (sz[x] >= B)
{
sz[x] = 0;
hq[++numProvince] = x;
for (auto child : curChild)
idProvince[child] = numProvince;
curChild.clear();
}
}
}
for (auto child : curChild)
d[child] = x;
sz[x]++;
if (sz[x] >= B)
{
sz[x] = 0;
hq[++numProvince] = x;
idProvince[x] = numProvince;
}
else if (!par)
idProvince[x] = numProvince;
}
int get(int x)
{
return x == d[x] ? x : d[x] = get(d[x]);
}
int main()
{
ios::sync_with_stdio(0);
freopen("royal.in", "r", stdin);
freopen("royal.out", "w", stdout);
int x, y;
cin >> n >> B;
for (int i = 1; i <= n; i++)
d[i] = i;
for (int i = 1; i < n; i++)
{
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
visit(1, 0);
cout << numProvince << endl;
for (int i = 1; i <= n; i++)
{
if (!idProvince[i])
idProvince[i] = idProvince[get(i)];
cout << idProvince[i] << ' ';
}
cout << endl;
for (int i = 1; i <= numProvince; i++)
cout << hq[i] << ' ';
}