QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#474170 | #6745. Delete the Tree | k1nsom | WA | 0ms | 13632kb | C++17 | 1.4kb | 2024-07-12 16:29:37 | 2024-07-12 16:29:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define N 200005
int n, k, m, tot, top, ctr = -1, si[N] = {0};
bool vis[N];
vector<int> e[N], ans[N];
void dfs(int u, int fa) // 找重心
{
si[u] = 1;
int mss = 0; // max subtree size
for (auto to : e[u])
if (!vis[to] && to != fa)
{
dfs(to, u);
if (ctr != -1)
return;
si[u] += si[to];
mss = max(mss, si[to]);
}
mss = max(mss, tot - si[u]);
if (mss <= tot / 2)
{
ctr = u;
si[fa] = tot - si[u];
}
}
void func(int u, int dis)
{
vis[u] = 1;
ans[dis].push_back(u);
top = max(top, dis);
for (auto to : e[u])
if (!vis[to])
{
tot = si[to];
ctr = -1;
dfs(to, 0);
func(ctr, dis + 1);
}
}
signed main()
{
cin >> n;
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
tot = n;
ctr = -1;
dfs(1, 0);
func(ctr, 1);
cout << top << endl;
for (int i = 1; i <= top; i++)
{
cout << ans[i].size() << ' ';
for (auto to : ans[i])
cout << to << ' ';
cout << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 13632kb
input:
5 1 2 1 3 1 4 4 5
output:
3 1 1 3 2 3 5 1 4
result:
wrong answer