QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474095#6745. Delete the Treek1nsomWA 3ms13112kbC++171.6kb2024-07-12 16:04:332024-07-12 16:04:34

Judging History

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

  • [2024-07-12 16:04:34]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:13112kb
  • [2024-07-12 16:04:33]
  • 提交

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 (!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: 13112kb

input:

5
1 2
1 3
1 4
4 5

output:

5
1 5 
2 
3 
5 
4 

result:

wrong answer