QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#388769#6745. Delete the Treek1nsomWA 0ms13820kbC++171.6kb2024-04-13 19:32:342024-04-13 19:32:34

Judging History

你现在查看的是最新测评结果

  • [2024-04-13 19:32:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:13820kb
  • [2024-04-13 19:32:34]
  • 提交

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;
}

Details

Tip: Click on the bar to expand more detailed information

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