QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#388801#6745. Delete the Treek1nsomWA 4ms13052kbC++171.9kb2024-04-13 19:56:132024-04-13 19:56:13

Judging History

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

  • [2024-04-13 19:56:13]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:13052kb
  • [2024-04-13 19:56:13]
  • 提交

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 (!vis[to] && !del[to] && to != fa)
        {
            check = 1;
            dfs2(to, u);
        }
    if (!check)
    {
        if (u == -1)
        {
            cout << u << ' ' << fa << endl;
        }
        ans[cnt].push_back(u);
        del[u] = 1;
    }
}
void func(int u)
{
    cnt++;
    bool ok = 1;
    for (auto to : e[u])
        if (!del[to] && !vis[to])
        {
            ok = 0;
            dfs2(to, u);
        }
    if (ok)
        cnt--;
    vis[u] = 1;
    for (auto to : e[u])
        if (!del[to] && !vis[to])
        {
            tot = si[to];
            ctr = -1;
            dfs(to);
            func(ctr);
        }
    if (!del[u])
    {
        del[u] = 1;
        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, 0);
    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: 100
Accepted
time: 4ms
memory: 13020kb

input:

5
1 2
1 3
1 4
4 5

output:

3
3 2 3 5 
1 4 
1 1 

result:

ok 

Test #2:

score: 0
Accepted
time: 2ms
memory: 13052kb

input:

500
183 443
32 443
334 443
254 443
331 443
348 443
54 443
430 443
275 443
410 443
360 443
443 468
140 443
179 443
93 443
327 443
128 443
365 443
122 443
43 443
46 443
399 443
398 443
269 443
130 443
227 443
412 443
61 443
295 443
98 443
30 443
197 443
397 443
95 443
192 443
266 443
48 443
310 443
28...

output:

2
499 183 32 334 254 331 348 54 430 275 410 360 468 140 179 93 327 128 365 122 43 46 399 398 269 130 227 412 61 295 98 30 197 397 95 192 266 48 310 283 127 123 7 154 317 302 158 65 218 306 191 309 210 20 190 204 484 182 429 362 99 92 347 39 488 58 115 228 8 346 111 386 498 408 259 289 333 256 352 26...

result:

ok 

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 13028kb

input:

500
80 180
80 254
1 180
80 337
180 323
80 248
180 205
80 189
180 480
80 330
180 454
80 498
142 180
80 193
180 346
80 89
180 389
80 125
180 232
80 93
180 228
80 327
180 357
80 417
180 362
80 278
180 316
80 312
163 180
80 310
176 180
80 463
180 210
80 478
180 294
80 185
124 180
80 143
180 339
80 253
1...

output:

3
498 1 323 205 480 454 142 346 389 232 228 357 362 316 163 176 210 294 124 339 223 409 175 474 181 81 140 301 134 215 471 4 122 456 160 484 331 318 22 69 105 342 219 363 446 194 408 25 101 84 407 60 488 348 157 358 211 423 169 403 303 499 486 286 436 356 493 190 47 366 347 90 213 264 75 398 102 70 ...

result:

wrong answer Integer -1 violates the range [1, 500]