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
|
void solve() {
int n, x, y;
cin >> n;
vvi ma(n);
rep(i, 1, n - 1) {
cin >> x >> y;
ma[x - 1].push_back(y - 1);
ma[y - 1].push_back(x - 1);
}
map<int, int> s;
map<int, map<int, vi>> s2;
int maxx = INT_MIN;
vector<pii> tem(n);
vi depth(n);
auto dfs = [&](this auto&& dfs, int x, int pa, int d) -> void {
s[d]++;
depth[x] = d;
int cnt = 0;
tem[x].first = pa;
for (int& p : ma[x]) {
if (p == pa) continue;
cnt++;
dfs(p, x, d + 1);
}
if (s.count(d + 1) && cnt == s[d + 1])
maxx = max(maxx, cnt + 1);
else
maxx = max(maxx, cnt);
return;
};
dfs(0, -1, 0);
for (auto& [x, y] : s) {
maxx = max(maxx, y);
}
cout << maxx << endl;
int md = sz(s);
vvi ma2(md);
rep(i, 0, n - 1) ma2[depth[i]].push_back(i);
tem[0].second = 0;
rep(i, 1, md - 1) {
sort(all(ma2[i]), [&](const int& x, const int& y) { return tem[tem[x].first].second < tem[tem[y].first].second; });
vector<bool> vis(maxx, false);
rep(j, 0, sz(ma2[i]) - 1) { vis[(tem[tem[ma2[i][j]].first].second - j + maxx) % maxx] = true; }
int idx = 0;
while (vis[idx]) idx++;
rep(j, 0, sz(ma2[i]) - 1) { tem[ma2[i][j]].second = (j + idx + maxx) % maxx; }
}
vvi res(maxx);
rep(i, 0, n - 1) { res[tem[i].second].push_back(i); }
for (auto& p : res) {
cout << sz(p) << endl;
for (auto& q : p) cout << q + 1 << ' ';
cout << endl;
}
return;
}
|