QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#672938 | #6473. Cat and Mouse | guosoun | AC ✓ | 3154ms | 14508kb | C++17 | 2.1kb | 2024-10-24 19:57:28 | 2024-10-24 19:57:28 |
Judging History
answer
#include <bits/stdc++.h>
template <class T>
void chkmin(T &x, T y) {
if (x > y) x = y;
}
using ll = long long;
void mian() {
int n, x;
std::cin >> n >> x;
std::vector<std::vector<std::pair<int, int>>> g(n + 1);
std::vector<int> fi(n + 1), se(n + 1), dep(n + 1), fa(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
g[u].emplace_back(i * 2 + 2, v);
g[v].emplace_back(i * 2 + 3, u);
}
for (int i = 1; i <= n; i++) {
std::sort(g[i].begin(), g[i].end(), std::greater<>());
fi[i] = g[i][0].second, se[i] = g[i].size() > 1 ? g[i][1].second : 0;
}
std::vector<std::vector<std::pair<int, int>>> ng((n - 1) * 2 + 2);
std::vector<int> dis((n - 1) * 2 + 2, 1e9);
for (int u = 1; u <= n; u++) {
for (auto [k, v] : g[u]) {
int w = fi[v] == u ? se[v] : fi[v];
if (w) {
int wk = w == fi[v] ? g[v][0].first : g[v][1].first;
ng[wk].emplace_back(k, 1);
if (fi[w] == v) {
if (fi[v] == w) ng[wk ^ 1].emplace_back(k, 4);
// else ng[k ^ 1].emplace_back(k, 3);
}
} else dis[k] = 0;
}
}
std::queue<int> q;
for (int i = 1; i <= (n - 1) * 2 + 1; i++)
if (dis[i] == 0) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto [v, w] : ng[u])
if (dis[v] > dis[u] + w) dis[v] = dis[u] + w, q.push(v);
}
for (auto [k, v] : g[1]) {
if (v == x) {
std::cout << dis[k] << '\n';
return;
}
}
auto dfs = [&](auto &self, int u, int ff, int d) -> void {
dep[u] = d, fa[u] = ff;
for (auto [w, v] : g[u])
if (v != ff) self(self, v, u, d + 1);
};
dfs(dfs, 1, 0, 0);
int ans = 1e9;
for (int i = 0; i <= n; i++) {
for (auto [w, y] : g[x])
if (dep[y] + (y != fa[x]) <= i) {
chkmin(ans, i + dis[w ^ 1]);
}
x = fi[x];
}
std::cout << ans << '\n';
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
// freopen("toptree.in", "r", stdin);
// freopen("toptree.out", "w", stdout);
int t;
std::cin >> t;
while (t--) mian();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2964ms
memory: 14300kb
input:
100 50000 11576 13519 31672 47035 48424 15051 40308 19661 34367 9452 17569 390 9986 26705 26923 37100 10713 25643 42676 9267 19233 49342 37739 6298 3807 5751 22924 25960 4439 33941 13190 12456 3487 29455 14728 49187 6717 36264 16848 46193 37225 30433 49509 2672 30522 12241 13826 42170 21723 19926 39...
output:
6560 12932 14271 19557 38634 46926 12218 21212 19279 8468 21912 2494 21988 15817 26345 16510 5965 6981 10344 3792 38960 8434 24402 26580 44114 5864 28880 14774 8806 23052 2964 34968 14304 9782 27897 23955 1070 15266 1734 17352 13635 9744 18308 32312 14708 7506 18284 8966 12870 2692 20240 34538 13611...
result:
ok 100 lines
Test #2:
score: 0
Accepted
time: 3154ms
memory: 14508kb
input:
100 50000 10844 9849 10014 30855 14044 41255 5689 741 27472 2188 15317 44247 15667 25250 20860 17386 30004 31283 6520 18517 29963 424 16058 27157 7702 43245 44247 421 2476 15880 25168 27347 29496 20263 27372 20340 36923 33827 16402 11332 23340 38182 3577 9347 9560 10735 27960 31533 1572 40061 49158 ...
output:
17006 46632 41698 43692 48660 366 530 121 823 516 278 601 306 971 531 498 560 424 475 798 718 119 81 567 578 74 993 354 354 216 323 572 372 105 386 626 330 224 879 387 736 163 524 485 477 650 943 555 861 1656 586 355 314 324 182 133 236 288 638 830 68 494 254 476 262 578 734 552 710 698 341 286 98 6...
result:
ok 100 lines
Test #3:
score: 0
Accepted
time: 2216ms
memory: 14216kb
input:
100 50000 48420 5321 1 26077 5321 26077 24577 26077 43563 26077 38392 26077 2558 26077 27287 26077 39040 26077 7727 26077 12052 26077 40703 26077 33984 26077 40937 26077 13983 26077 48098 26077 34559 26077 42966 26077 38658 26077 11124 26077 4692 26077 43857 26077 9986 26077 5102 26077 2025 26077 46...
output:
222 512 403 336 556 790 725 575 100 607 229 521 187 432 676 793 115 664 568 617 739 119 395 695 507 682 530 475 594 562 82 620 462 590 603 185 242 60 282 494 618 119 436 158 796 521 207 271 350 795 736 600 552 610 285 637 728 126 126 426 606 583 532 298 49 711 493 527 441 544 705 20 588 688 512 287 ...
result:
ok 100 lines
Test #4:
score: 0
Accepted
time: 1868ms
memory: 13656kb
input:
100 50000 9201 9201 47027 47027 47237 47237 3995 3995 18198 18198 39240 39240 17258 17258 19559 19559 19209 19209 19537 19537 25582 25582 21308 21308 39528 39528 47539 47539 40425 40425 18383 18383 12971 12971 11770 11770 34397 34397 36428 36428 3477 3477 848 848 38832 38832 10459 10459 48840 48840 ...
output:
13123 5291 13991 6420 6162 12366 3958 2637 5949 5637 1871 6440 1671 4399 1491 6384 9519 2068 5010 7756 7120 6378 4966 7437 4682 4052 12105 8427 8178 7437 1080 6160 9753 6900 12003 13017 3866 10031 6816 1769 3182 9836 4329 3403 9972 3536 10412 9880 5769 1552 8282 4943 3884 3360 1393 2153 6058 7908 10...
result:
ok 100 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
2 2 1 1 2 2 2 1 2
output:
1 0
result:
ok 2 lines