QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#665981 | #6473. Cat and Mouse | SkyMaths | AC ✓ | 440ms | 41692kb | C++20 | 3.9kb | 2024-10-22 16:07:50 | 2024-10-22 16:07:55 |
Judging History
answer
#pragma GCC optimize("-Ofast", "-finline", "-funroll-loops", "-fno-stack-protector")
#include <bits/stdc++.h>
#define rep(i,l,r) for (int i(l), i##end(r); i <= i##end; ++i)
#define per(i,r,l) for (int i(r), i##end(l); i >= i##end; --i)
using namespace std;
bool _Mbe; int subtask_id; char ibuf[1 << 25], *iS, *iT;
#define getc() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, 1 << 25, stdin), (iS == iT ? EOF : *iS++) : *iS++)
template <typename T> inline void read(T &x) { x = 0; bool f = 0; char ch = getc(); while (ch < '0' || ch > '9') f ^= ch == '-', ch = getc(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getc(); if (f) x = -x;}
template <typename Tx, typename ...Ty> inline void read(Tx &x, Ty &...y) { read(x); read(y...);}
template <typename T> inline void Write(T x) { if (x > 9) Write(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void write(T x, char ch = '\n') { if (x < 0) { putchar('-'); x = -x;} Write(x); putchar(ch);}
template <typename T> inline void cmin(T &x, T y) { (x > y) && (x = y);}
template <typename T> inline void cmax(T &x, T y) { (x < y) && (x = y);}
#define File(filename) freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout)
/* - - - all above template - - - */
#define PII pair <int, int>
const int N = 5e4 + 9;
const int Ecnt = N * 4;
const int inf = 1e9;
int n, x, ecnt(1);
int head[N], dep[N], fa[N], suf[N][2];
int dis[Ecnt];
struct Edge {int from, to, nxt; } e[Ecnt];
priority_queue <PII, vector <PII>, greater<PII> > pq;
void add_edge(int u, int v) {
e[++ecnt] = (Edge) {u, v, head[u]};
head[u] = ecnt;
}
// 返回的是边的编号
int F(int x, int u) {return suf[x][e[suf[x][0]].to == u];}
void dfs(int u, int lst) {
fa[u] = lst;
for (int i = head[u]; i; i = e[i].nxt) {
if (int v = e[i].to; v != lst) {
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
}
void upd(int u, int v) {
if (dis[u] > v) {
// printf(" id = %d, u = %d, x = %d, dis = %d\n", u, e[u].from, e[u].to, v);
dis[u] = v;
pq.push(make_pair(v, u));
}
}
void skymaths() {
read(n, x);
ecnt = 1; memset(head, 0, sizeof(head[0]) * (n + 1));
for (int i = 1, u, v; i < n; ++i) {
read(u, v);
add_edge(u, v); add_edge(v, u);
}
dfs(1, 0);
rep (i, 1, n) {
suf[i][0] = suf[i][1] = 0;
for (int j = head[i], t = 0; j && t < 2; j = e[j].nxt) {
// 因为这本身是降序的
suf[i][t++] = j;
}
}
// 代表 u 在 from,x 在 to
fill(dis + 1, dis + ecnt + 1, inf);
dis[0] = -inf;
int X = x;
rep (i, 0, 2 * n) {
int to = e[suf[X][0]].to;
if ((to == fa[X] && dep[to] <= i) || (to != fa[X] && dep[to] + 1 <= i)) {
upd(suf[X][0] ^ 1, i);
}
X = to;
}
while (!pq.empty()) {
PII top = pq.top(); pq.pop();
int s = top.second; if (dis[s] != top.first) continue;
int u = e[s].from;
int x = e[s].to;
int id = F(x, u); int w = e[id].to;
upd(id, dis[s] + 1);
if (F(x, x) == id && F(w, w) == (id ^ 1)) upd(id ^ 1, dis[s] + 4);
}
int ans(inf);
rep (i, 2, ecnt) {
if (F(e[i].to, e[i].from) == 0) {
// cerr << "End id = " << i << endl;
cmin(ans, dis[i]);
}
}
printf("%d\n", ans);
}
/*
start coding
at 21:17
finish coding
finish debugging
*/
bool _Med; double start_time;
signed main() {
start_time = clock();
cerr << "memories= " << (&_Mbe - &_Med) / 1048576.0 << "MiB" << endl;
/*
#ifdef DEBUG
freopen("a.in", "r", stdin);
#else
File("toptree");
#endif
*/
int T = 1;
scanf("%d", &T);
while (T--) {
skymaths();
}
cerr << "time= " << (clock() - start_time) / CLOCKS_PER_SEC << "sec" << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 440ms
memory: 41500kb
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: 348ms
memory: 41692kb
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: 347ms
memory: 41644kb
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: 391ms
memory: 41296kb
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: 1ms
memory: 5924kb
input:
2 2 1 1 2 2 2 1 2
output:
1 0
result:
ok 2 lines