QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#388769 | #6745. Delete the Tree | k1nsom | WA | 0ms | 13820kb | C++17 | 1.6kb | 2024-04-13 19:32:34 | 2024-04-13 19:32:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
#define endl '\n'
int n, ctr = -1, cnt = 0, tot, si[N] = {0};
bool del[N], vis[N];
vector<int> e[N], ans[N];
void dfs(int u, int fa = 0)
{
si[u] = 1;
int mss = 0;
for (auto to : e[u])
if (!del[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 dfs2(int u, int fa)
{
bool check = 0;
for (auto to : e[u])
if (!del[to] && to != fa)
{
check = 1;
dfs2(to, u);
}
if (!check)
{
ans[cnt].push_back(u);
del[u] = 1;
}
}
void func(int u)
{
cnt++;
for (auto to : e[u])
if (!del[to])
{
dfs2(to, u);
}
del[u] = 1;
for (auto to : e[u])
if (!del[to])
{
tot = si[to];
ctr = -1;
dfs(to);
func(ctr);
}
ans[cnt].push_back(u);
}
signed main()
{
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
tot = n;
ctr = -1;
dfs(1);
func(ctr);
cout << cnt << endl;
for (int i = 1; i <= cnt; cout << endl, i++)
{
cout << ans[i].size() << ' ';
for (auto v : ans[i])
cout << v << ' ';
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 13820kb
input:
5 1 2 1 3 1 4 4 5
output:
2 3 2 3 5 2 4 1
result:
wrong answer