QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751140 | #6745. Delete the Tree | XfJbUhpyzgaW# | ML | 1ms | 4900kb | C++17 | 1.3kb | 2024-11-15 17:16:52 | 2024-11-15 17:16:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (int i = (a), iee = (b); i <= iee; i++)
#define R(i, a, b) for (int i = (a), iee = (b); i >= iee; i--)
const int N = 510;
int n;
int G[N][N];
vector<int> e[N], te[N];
int tag[N], del[N];
vector<int> tmp;
vector<vector<int>> res;
void dfs(int x, int fa) {
tag[x] = 0;
int deg = 0;
bool ok = true;
F(i, 1, n) if (!del[i] && G[x][i] && i != fa) {
deg++, dfs(i, x);
if (tag[i]) ok = false;
}
if (ok && deg <= 2) tmp.push_back(x), tag[x] = 1;
}
bool solve() {
int rt = 0;
F(i, 1, n) if (!del[i]) rt = i;
if (!rt) return 0;
F(i, 1, n) tag[i] = 0;
tmp.clear();
dfs(rt, 0);
for (int v: tmp) {
vector<int> nei;
F(i, 1, n) if (G[v][i] && !del[i]) nei.push_back(i);
for (int x: nei) for (int y: nei) if (x != y) G[x][y] = 1;
}
for (int v: tmp) del[v] = 1;
res.push_back(tmp);
return 1;
}
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
cin >> n;
F(i, 2, n) { int x, y; cin >> x >> y; G[x][y] = G[y][x] = 1; }
while (solve());
cout << res.size() << "\n";
for (auto t: res) {
cout << t.size() << " ";
for (int x: t) cout << x << " ";
cout << "\n";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
5 1 2 1 3 1 4 4 5
output:
3 3 2 3 4 1 1 1 5
result:
ok
Test #2:
score: 0
Accepted
time: 1ms
memory: 4900kb
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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...
result:
ok
Test #3:
score: 0
Accepted
time: 1ms
memory: 4816kb
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 3 5 7 8 10 15 18 20 21 23 27 28 32 33 34 35 43 46 48 50 51 53 54 55 56 61 62 63 64 65 68 72 76 77 78 79 83 86 87 88 89 91 93 94 95 96 98 106 109 111 114 118 120 123 125 126 127 128 130 131 135 137 138 139 143 144 146 147 149 150 151 152 154 156 158 159 161 162 164 165 167 168 171 172 174 177 1...
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 4632kb
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:
9 250 232 220 354 372 278 260 201 497 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 5...
result:
ok
Test #5:
score: -100
Memory Limit Exceeded
input:
500 147 209 104 147 13 209 209 466 104 485 17 104 13 214 13 179 151 466 176 466 130 485 286 485 17 359 17 178 214 486 55 214 179 350 179 327 151 167 151 498 146 176 102 176 99 130 130 232 286 294 286 389 56 359 330 359 178 488 178 441 440 486 210 486 55 157 55 458 237 350 350 352 327 371 317 327 167...