QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#474087 | #6745. Delete the Tree | k1nsom | WA | 3ms | 13316kb | C++17 | 1.7kb | 2024-07-12 16:02:32 | 2024-07-12 16:02:33 |
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 = 0, ctr = -1, si[N] = {0};
bool vis[N], del[N], instk[N];
vector<int> e[N], temp, ans[N];
void dfs(int u, int fa) // 找重心
{
si[u] = 1;
int mss = 0; // max subtree size
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, int len)
{
if (len > k)
return;
if (!instk[u] && len % 2 == 0)
{
instk[u] = 1;
ans[top].push_back(u);
}
for (auto to : e[u])
if (!del[to] && to != fa)
dfs2(to, u, len + 1);
}
void func(int u)
{
top++;
ans[top].push_back(u);
for (auto to : e[u])
if (!del[to])
dfs2(to, u, 1);
del[u] = 1;
for (auto to : e[u])
if (!del[to])
{
tot = si[to];
ctr = -1;
dfs(to, 0);
func(ctr);
}
}
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);
cout << top << endl;
for (int i = 1; i <= top; i++)
{
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: 3ms
memory: 13316kb
input:
5 1 2 1 3 1 4 4 5
output:
5 1 2 3 5 4
result:
wrong output format Unexpected end of file - int32 expected