QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#397090#6745. Delete the TreeVictorYuanWA 1ms3792kbC++142.3kb2024-04-23 16:21:272024-04-23 16:21:28

Judging History

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

  • [2024-04-23 16:21:28]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3792kb
  • [2024-04-23 16:21:27]
  • 提交

answer

#include <bits/stdc++.h> 

const int maxn = 505;

int n, cnt;
std::vector<int> e[maxn], tmp[maxn];
std::vector<std::vector<int>> ans;
int del[maxn];
int siz[maxn],mx_s[maxn];
std::mt19937 rnd;

void dfs1(int x,int fa) {
  siz[x] = 1;
  for(auto v:e[x]) {
    if(v != fa) {
      dfs1(v,x);
      siz[x] += siz[v];
    }
  }
  mx_s[x] = n - siz[x] - cnt;
  for(auto v:e[x]) {
    if(v != fa)
    mx_s[x] = std::max(mx_s[x],siz[v]);
  }
}
int getroot() {
  int x = -1;
  for (int i = 1; i <= n; ++i) if (e[i].size() == 1) { x = i;break; }
  for (int i = 1; i <= n; ++i) if (del[i] == 0) {x = i;break;}
  //dep[x] = 0;
  dfs1(x,0);
  int mn = 1e9,rt = -1;
  for(int i = 1;i <= n;i ++) {
    if(mx_s[i] < mn && del[i] == 0) {
      mn = mx_s[i];
      rt = i;
    }
  }
  return rt;
  assert(false);
}

void dfs(const int u, int pre = -1) {
  for (auto v : e[u]) if (v != pre) {
    dfs(v, u);
  }
  if (e[u].size() == 2) {
    int v = e[u][0];
    if (v == pre) v = e[u][1];
    if (!del[v]) {
      del[u] = 1;
      ans.back().push_back(u);
    }
  } else if (e[u].size() < 2 && pre != -1) {
    del[u] = 1;
    ans.back().push_back(u);
  } else if (e[u].size() < 2) {
    if (e[u].size() == 0 || (e[u].size() == 1 && !del[e[u][0]])) {
      del[u] = 1;
      ans.back().push_back(u);
    }
  }
}

int main() {  
  rnd.seed(time(nullptr));
  std::cin >> n;
  for (int u, v, i = 1; i < n; ++i) {
    std::cin >> u >> v;
//    u = i + 1;
//    v = std::max(1, u - 1 - int(rnd() % 3));
    e[u].push_back(v);
    e[v].push_back(u);
  }
  while (cnt != n) {
    ans.push_back(std::vector<int>());
    dfs(getroot());
    for (int i = 1; i <= n; ++i) tmp[i].clear();
    for (int u = 1; u <= n; ++u) if (!del[u]) {
      for (auto v : e[u]) if (!del[v]) {
        tmp[u].push_back(v);
      }
    } else if (del[u] == 1) {
      for (auto v : e[u]) {
        for (auto k : e[u]) if (v != k) {
          tmp[v].push_back(k);
        }
      }
      del[u] = -1;
    }
    for (int i = 1; i <= n; ++i) e[i].swap(tmp[i]);
    cnt += ans.back().size();
  }
  std::cout << ans.size();
//  if(ans.size() - 10 >= 5) return -1;
//  assert(ans.size() <= 10);
  for (auto &i : ans) {
    std::cout << '\n' << i.size(); for (auto j : i) std::cout << ' ' << j;
  }
  std::cout << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3772kb

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: 0ms
memory: 3716kb

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: 0
Accepted
time: 1ms
memory: 3644kb

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:

ok 

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3792kb

input:

500
387 488
301 488
301 413
13 413
13 265
176 265
176 398
74 398
74 241
241 415
386 415
386 448
210 448
210 285
147 285
147 264
19 264
19 314
314 335
54 335
54 261
261 484
425 484
350 425
156 350
156 164
164 420
8 420
8 309
230 309
230 441
408 441
183 408
183 410
204 410
204 318
151 318
151 328
328 ...

output:

13
250 387 301 13 176 74 415 448 285 264 314 54 484 350 164 8 230 408 410 318 328 494 111 21 477 273 133 310 78 480 192 417 471 266 432 424 269 12 308 226 333 234 104 70 434 457 406 179 473 239 227 190 365 79 81 123 416 483 80 338 467 5 470 22 188 212 347 476 332 341 55 270 253 152 362 371 404 89 47...

result:

wrong answer Integer 13 violates the range [0, 10]