QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#558863 | #8220. 众生之门 | The_cosmos | 10 | 685ms | 18100kb | C++14 | 4.1kb | 2024-09-11 18:56:36 | 2024-09-11 18:56:36 |
Judging History
answer
#include <bits/stdc++.h>
#define REP(i, first, last) for (int i = (first); i <= (last); ++i)
#define DOW(i, first, last) for (int i = (first); i >= (last); --i)
using namespace std;
// #define int long long
#define pb emplace_back
#define ull unsigned long long
const int N = 5e5 + 5, M = 2e5 + 5, Sg = 26;
const int mod1 = 1e9 + 9, mod2 = 998244353;
const long long inf = 1e18;
const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const int P = 1ll << 21, S = P - 1;
struct Graph {
struct edge {
int from, to, next;
int w;
} e[N << 1];
int cnt, n, head[N];
void ins(int u, int v, int w = 0) {
e[++cnt] = {u, v, head[u], w}, n = max({n, u, v}), head[u] = cnt;
}
void twi(int u, int v, int w = 0) { ins(u, v, w), ins(v, u, w); }
void clear() {
for (int i = 1; i <= cnt; i++) head[e[i].from] = 0;
cnt = 0;
}
#define FEG(u, v, W, G, i) \
for (int i = G.head[u], v = G.e[i].to, W = G.e[i].w; i; \
i = G.e[i].next, v = G.e[i].to, W = G.e[i].w)
} G;
int n, s, t;
int pos[N];
struct LCA {
int dfn[N], dfncnt = 0, pre[N], siz[N], f[N][25], dep[N];
void dfs(int u, int fa) {
dfn[u] = ++dfncnt;
dep[u] = dep[fa] + 1;
pre[dfncnt] = u;
f[u][0] = fa;
REP(i, 1, 20) f[u][i] = f[f[u][i - 1]][i - 1];
FEG(u, v, W, G, i) {
if (v == fa) continue;
dfs(v, u);
pre[++dfncnt] = u;
}
}
int logn[N << 1];
int st[N << 1][25];
void init() {
logn[0] = -1;
REP(i, 1, dfncnt) {
logn[i] = logn[i / 2] + 1;
st[i][0] = pre[i];
}
for (int j = 1; (1 << j) <= dfncnt; j++) {
for (int i = 1; i + (1 << j) <= dfncnt + 1; i++) {
int u = st[i][j - 1], v = st[i + (1 << (j - 1))][j - 1];
st[i][j] = ((dep[u] > dep[v]) ? v : u);
}
}
}
int lca(int l, int r) {
int ql = dfn[l], qr = dfn[r];
if (qr < ql) swap(qr, ql);
int k = logn[qr - ql + 1];
return (dep[st[ql][k]] < dep[st[qr - (1 << k) + 1][k]]
? st[ql][k]
: st[qr - (1 << k) + 1][k]);
}
int dist(int u, int v) {
int w = lca(u, v);
return dep[u] + dep[v] - 2 * dep[w];
}
} f;
bool check() {
REP(u, 1, n) {
int ans = 0;
FEG(u, v, w, G, i) {
ans ++;
}
if(ans == n - 1) return 1;
}
return 0;
}
mt19937_64 rnd(chrono :: system_clock().now().time_since_epoch().count());
int rand(int l, int r) {
return rnd() % (r - l + 1) + l;
}
int get(int x) {
return f.dist(pos[x], pos[x - 1]) ^ f.dist(pos[x], pos[x + 1]);
}
bool vis[N];
int ans = 5, opt[N];
void answer() {
int res = 0;
REP(i, 1, n - 1) res ^= f.dist(pos[i], pos[i + 1]);
if(res < ans) {
ans = res;
REP(i, 1, n) opt[i] = pos[i];
}
}
void dfs(int u) {
if(u > n - 1) return answer();
REP(i, 1, n) {
if(!vis[i] && f.dist(i, pos[u - 1]) > 3) continue;
pos[u] = i, vis[i] = 1;
dfs(u + 1), vis[i] = 0;
}
}
void Solve() {
cin >> n >> s >> t;
// cerr << "qwq";
G.clear(), f.dfncnt = 0;
REP(i, 2, n) {
int u, v;
cin >> u >> v;
G.twi(u, v);
}
pos[1] = s, pos[n] = t;
int l = 1;
REP(i, 2, n - 1) {
while (l == s || l == t) l++;
pos[i] = l, l ++;
}
// REP(i, 1, n) cerr << pos[i] << ' ';
// cerr << '\n';
f.dfs(1, 0), f.init();
int res = 0;
REP(i, 1, n - 1) res ^= f.dist(pos[i], pos[i + 1]);
if(check()) {
REP(i, 1, n) cout << pos[i] << ' ';
cout << '\n';
return ;
}
if(n < 9) {
REP(i, 1, n) vis[i] = 0;
vis[s] = vis[t] = 1;
ans = 5, dfs(2);
REP(i, 1, n) cout << opt[i] << ' ';
cout << '\n';
return ;
}
while(res > 1) {
int x = rand(2, n - 1), y = rand(2, n - 1);
while(x == y) y = rand(2, n - 1);
res ^= get(x) ^ get(y);
swap(pos[x], pos[y]);
res ^= get(x) ^ get(y);
}
REP(i, 1, n) cout << pos[i] << ' ';
cout << '\n';
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
Solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 17908kb
input:
114 6 5 6 2 6 1 6 4 5 3 1 6 4 6 3 6 2 4 4 1 6 4 1 5 5 3 6 6 1 5 2 1 2 4 6 2 4 3 2 6 6 1 3 6 5 3 1 6 4 2 2 5 6 3 1 5 3 2 4 1 5 4 3 6 3 4 3 4 2 3 1 4 4 3 6 3 1 2 3 6 3 4 3 1 6 5 1 5 3 2 1 2 4 2 2 3 5 2 6 1 4 2 1 5 2 4 1 6 2 3 6 6 5 1 4 2 6 5 1 3 2 6 3 2 4 4 1 2 4 3 4 1 4 6 2 5 3 5 4 6 1 4 6 2 5 4 6 1 ...
output:
5 1 1 1 2 6 3 1 1 1 1 6 6 1 1 2 3 1 6 1 1 1 1 1 3 1 1 3 5 1 3 1 3 4 3 1 1 2 4 1 3 1 4 5 2 1 1 1 1 1 4 5 1 1 2 5 1 4 2 3 1 2 1 1 1 1 5 1 1 1 1 2 3 3 1 1 1 1 1 2 1 1 1 2 4 3 1 2 5 6 4 5 1 1 1 1 3 1 1 1 3 5 4 1 3 2 3 1 1 1 4 1 1 2 4 3 1 3 2 5 1 1 1 2 5 1 2 3 1 5 1 1 1 2 3 4 1 1...
result:
FAIL Your output should be a permutation in test case 1.
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 116ms
memory: 17988kb
input:
14190 43 27 2 42 3 30 36 11 24 21 22 13 8 22 30 31 29 35 1 10 6 2 23 28 17 2 26 7 37 5 19 38 43 33 39 4 28 33 7 25 31 15 1 32 18 34 27 35 12 19 32 20 17 37 42 26 34 39 10 12 27 24 43 18 6 16 9 38 9 14 15 14 41 25 3 40 13 16 8 36 41 20 5 21 40 11 29 41 24 38 21 6 20 14 26 1 6 7 17 16 39 36 8 18 36 11...
output:
27 5 3 43 1 38 7 6 16 34 11 26 20 14 15 9 17 18 19 37 21 22 23 24 13 12 28 29 30 31 32 33 10 35 36 25 8 39 40 41 42 4 2 24 1 2 3 20 5 6 7 37 36 12 11 33 13 14 39 16 15 19 18 17 21 31 23 25 26 27 9 29 30 22 32 4 41 35 10 8 28 40 34 38 5 7 2 16 4 6 1 20 9 10 19 12 13 14 27 3 17 18 11 8 21 23 24 25 2...
result:
FAIL Your output should be a permutation in test case 284.
Subtask #4:
score: 0
Wrong Answer
Test #18:
score: 0
Wrong Answer
time: 505ms
memory: 17940kb
input:
32752 15 3 4 14 12 4 12 1 10 9 13 7 6 12 5 1 12 9 15 7 9 8 12 2 6 11 6 9 3 6 10 13 12 2 10 11 10 5 1 4 12 11 4 6 2 13 6 5 9 6 8 13 6 3 4 8 13 7 15 3 6 15 10 4 2 8 5 10 3 1 3 15 2 8 4 12 9 7 8 11 8 6 13 8 12 14 8 6 12 15 5 7 8 14 10 13 11 13 13 5 2 14 15 8 15 1 6 2 7 15 9 13 15 3 6 13 15 4 12 5 15 10...
output:
3 1 2 5 6 7 8 9 10 11 12 13 14 15 4 12 4 13 5 1 10 7 8 11 6 3 9 2 3 1 2 4 5 7 8 9 10 11 12 13 14 15 6 5 4 2 6 10 3 8 9 11 1 14 13 12 15 7 10 1 11 3 4 5 6 12 9 2 7 13 14 15 8 11 4 2 6 10 1 9 7 5 8 3 11 1 2 3 4 5 6 7 8 10 12 13 14 15 9 14 5 11 10 8 4 9 13 6 3 1 2 7 12 5 4 2 1 6 7 10 9 8 11 3 ...
result:
FAIL Your output should be a permutation in test case 416.
Subtask #5:
score: 0
Wrong Answer
Test #25:
score: 0
Wrong Answer
time: 685ms
memory: 18064kb
input:
36059 13 9 4 5 9 10 3 3 1 13 5 12 5 7 4 2 8 8 10 4 9 11 7 6 11 1 4 13 12 6 4 12 13 9 11 2 6 12 9 12 8 5 7 6 5 3 3 7 10 8 1 5 2 10 13 10 8 3 1 5 9 4 8 6 11 7 13 13 5 1 10 12 13 9 4 11 9 2 11 8 10 12 1 4 9 2 2 12 3 2 12 11 8 2 7 4 5 1 4 1 11 7 10 9 6 9 13 10 12 7 5 11 9 12 10 9 8 3 10 8 5 4 13 13 7 6 ...
output:
9 2 1 3 5 10 7 8 6 12 11 13 4 12 13 2 9 4 3 11 7 5 10 8 1 6 10 12 2 3 4 5 6 7 9 11 1 13 8 1 5 3 10 9 12 8 6 7 2 11 4 10 1 2 3 13 11 4 7 8 9 5 6 12 6 1 9 3 10 13 4 7 12 11 2 5 8 1 2 3 4 5 6 7 9 10 11 12 13 8 7 1 2 6 5 4 8 9 10 3 11 13 3 4 5 6 7 8 9 10 12 2 1 4 1 2 3 5 7 8 9 10 11 12 13 6 6 ...
result:
FAIL Your output should be a permutation in test case 260.
Subtask #6:
score: 10
Accepted
Test #34:
score: 10
Accepted
time: 5ms
memory: 17924kb
input:
10 1000 165 244 175 661 738 362 280 462 776 922 231 578 963 615 639 836 32 418 519 220 565 733 239 951 768 847 196 200 246 119 591 288 994 586 313 46 971 515 512 811 228 908 627 339 33 337 447 488 616 319 399 727 921 615 421 509 167 354 905 382 20 356 875 414 619 904 824 940 435 244 953 663 719 962 ...
output:
165 55 2 252 4 843 6 164 8 683 10 826 12 13 14 664 309 17 18 543 20 534 672 23 24 25 26 27 28 958 176 925 32 33 34 961 959 37 296 39 40 340 600 680 44 45 428 441 182 49 666 51 52 53 54 1 56 57 58 59 60 61 67 847 64 65 66 997 68 69 759 549 72 73 74 102 574 77 578 79 80 204 82 606 84 978 86 560 88 89 ...
result:
ok Answer correct!
Test #35:
score: 10
Accepted
time: 0ms
memory: 17956kb
input:
10 1000 382 266 590 318 797 98 35 830 950 354 905 784 998 709 853 583 165 498 288 727 822 759 576 543 193 715 883 839 847 872 255 61 995 187 125 742 575 697 621 939 711 248 445 683 848 907 171 215 511 807 196 453 166 930 231 716 327 96 866 680 909 549 188 554 33 273 486 74 32 577 37 573 376 148 709 ...
output:
382 1 2 3 4 5 6 7 8 9 10 11 12 721 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 953 746 35 36 710 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 103 58 59 60 61 62 63 64 65 66 67 68 859 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 846 86 87 88 89 90 731 92 93 146 95 96 97 98 99...
result:
ok Answer correct!
Test #36:
score: 10
Accepted
time: 5ms
memory: 17852kb
input:
10 1000 252 455 479 592 266 350 335 8 117 54 580 751 999 484 73 339 97 325 96 336 629 247 947 425 374 458 666 716 498 402 859 866 361 441 627 365 372 505 456 485 376 379 422 416 72 498 489 369 211 537 909 508 12 23 523 738 714 383 556 447 548 773 521 353 76 581 555 780 565 148 672 15 570 768 888 39 ...
output:
252 965 2 161 4 323 450 7 724 385 981 333 12 217 14 205 16 564 648 19 772 976 938 23 24 25 26 27 28 690 30 761 718 33 34 35 36 37 174 39 40 41 241 452 278 45 17 695 48 292 50 51 775 53 11 55 416 151 58 628 60 62 61 63 64 233 66 368 68 69 596 934 307 73 74 75 76 77 776 392 642 81 82 83 87 85 86 84 88...
result:
ok Answer correct!
Test #37:
score: 10
Accepted
time: 5ms
memory: 18000kb
input:
10 1000 304 234 651 949 290 646 953 375 867 994 71 810 114 466 924 223 583 869 947 766 627 169 100 616 847 801 20 138 886 364 320 941 866 188 408 606 762 366 842 325 934 184 122 918 736 773 348 289 876 770 913 227 274 176 727 87 18 280 305 595 774 884 441 582 328 711 829 836 891 503 45 971 804 500 6...
output:
304 1 2 553 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 827 26 27 28 368 30 31 32 33 439 35 36 37 38 39 40 41 191 43 44 45 46 47 48 49 390 51 52 150 54 55 56 57 58 59 60 61 62 63 64 65 66 956 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 435 87 895 89 90 91 92 93 94 95 515 864 9...
result:
ok Answer correct!
Test #38:
score: 10
Accepted
time: 0ms
memory: 18100kb
input:
10 1000 105 350 300 724 309 285 319 499 170 893 469 224 304 775 964 858 625 758 529 516 328 239 585 559 341 670 135 549 810 328 439 251 49 198 265 691 30 101 146 261 376 929 981 434 980 439 191 601 764 888 731 801 706 824 203 210 83 419 41 812 176 472 226 117 156 280 996 478 902 761 75 57 542 969 36...
output:
105 1 317 3 4 5 6 7 8 467 10 11 12 13 14 15 16 439 18 19 20 21 22 23 24 55 26 27 28 250 30 31 32 33 34 35 36 37 38 39 417 41 42 43 44 45 46 47 48 402 50 51 52 53 54 25 56 57 58 59 60 61 62 63 153 65 66 67 68 69 70 71 72 872 74 75 76 77 78 79 80 621 277 943 84 85 86 87 620 217 90 91 92 734 94 95 766 ...
result:
ok Answer correct!
Subtask #7:
score: 0
Skipped
Dependency #2:
0%