QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474170#6745. Delete the Treek1nsomWA 0ms13632kbC++171.4kb2024-07-12 16:29:372024-07-12 16:29:38

Judging History

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

  • [2024-07-12 16:29:38]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:13632kb
  • [2024-07-12 16:29:37]
  • 提交

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