QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#309153 | #7524. A Challenge For a Tourist | 8BQube# | TL | 2ms | 9772kb | C++20 | 3.5kb | 2024-01-20 15:11:58 | 2024-01-20 15:11:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const int INF = 2e9;
struct Edge {
int u, v, w;
bool operator<(const Edge &e) const {
return w < e.w;
}
} edges[200005];
struct Basis {
vector<int> basis;
void insert(int x) {
for (int i : basis)
x = min(x, x ^ i);
if (x > 0)
basis.pb(x);
}
bool check(int x) {
for (int i : basis)
x = min(x, x ^ i);
return x == 0;
}
void Union(const Basis &b) {
for (int i : b.basis)
insert(i);
}
} basis[200005];
vector<pii> G[200005];
vector<Basis> version[200005];
vector<int> timing[200005];
int depth[200005], boss[200005], sz[200005], up[200005];
int finds(int x) {
while (x != boss[x]) x = boss[x];
return x;
}
bool Union(int u, int v, int w, int type) {
if (finds(u) == finds(v)) return false;
if (type == 0) {
G[u].pb(pii(v, w));
G[v].pb(pii(u, w));
}
u = finds(u), v = finds(v);
if (sz[u] > sz[v]) swap(u, v);
boss[u] = v;
up[u] = w;
sz[v] += sz[v];
if (type == 1) {
version[v].pb(version[v].back());
timing[v].pb(w);
version[v].back().Union(version[u].back());
}
return true;
}
void dfs(int u, int f, int d) {
depth[u] = d;
for (auto [v, w] : G[u])
if (v != f)
dfs(v, u, d ^ w);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i)
cin >> edges[i].u >> edges[i].v >> edges[i].w;
sort(edges + 1, edges + m + 1);
iota(boss + 1, boss + n + 1, 1);
fill_n(sz + 1, n, 1);
for (int i = 1; i <= m; ++i)
Union(edges[i].u, edges[i].v, edges[i].w, 0);
for (int i = 1; i <= n; ++i)
if (finds(i) == i)
dfs(i, i, 0);
iota(boss + 1, boss + n + 1, 1);
fill_n(sz + 1, n, 1);
fill_n(up + 1, n, INF);
for (int i = 1; i <= n; ++i)
version[i].pb(Basis()), timing[i].pb(-1);
for (int i = 1; i <= m; ++i) {
if (!Union(edges[i].u, edges[i].v, edges[i].w, 1)) {
int r = finds(edges[i].u);
version[r].pb(version[r].back()), version[r].back().insert(depth[edges[i].u] ^ depth[edges[i].v] ^ edges[i].w);
timing[r].pb(edges[i].w);
}
}
int q;
cin >> q;
while (q--) {
int u, v, w;
cin >> u >> v >> w;
if (finds(u) != finds(v)) {
cout << "-1\n";
continue;
}
int path = depth[u] ^ depth[v], lower = 0;
while (u != v) {
if (sz[u] > sz[v]) swap(u, v);
lower = max(lower, up[u]);
u = boss[u];
}
auto check = [&](int t) {
if (t < lower) return false;
int cur = u;
while (t >= up[cur]) cur = boss[cur];
int p = upper_bound(ALL(timing[cur]), t) - timing[cur].begin() - 1;
return version[cur][p].check(path ^ w);
};
int l = 1, r = m + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (check(edges[mid].w)) r = mid;
else l = mid + 1;
}
if (l == m + 1) cout << "-1\n";
else cout << edges[l].w << "\n";
}
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7700kb
input:
6 6 5 6 3 6 2 2 1 2 1 2 3 2 2 4 3 4 5 4 3 1 3 1 1 3 3 1 3 5
output:
-1 2 4
result:
ok 3 number(s): "-1 2 4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7708kb
input:
2 1 1 2 1 1 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 7636kb
input:
5 10 1 2 4 2 2 0 1 1 7 2 1 7 4 1 1 5 5 1 4 1 4 5 2 6 1 1 2 2 4 7 10 1 4 3 1 2 5 3 2 1 3 5 5 2 4 0 3 2 0 2 1 2 2 3 6 5 1 7 2 3 3
output:
2 6 -1 -1 4 -1 6 -1 6 -1
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 7632kb
input:
10 20 3 5 667 2 5 71 2 9 47 7 9 941 9 1 564 2 1 892 7 1 627 2 2 989 8 9 978 1 3 936 1 1 807 8 4 564 6 9 518 5 4 896 2 9 607 3 9 453 1 3 402 10 8 935 7 3 826 1 6 707 40 3 10 164 2 10 248 5 6 708 5 9 764 1 9 711 3 8 377 9 1 640 7 1 554 3 1 504 4 9 761 1 8 111 5 8 296 4 2 97 10 4 896 5 1 232 5 8 154 7 ...
output:
-1 941 -1 -1 -1 941 941 936 892 -1 896 -1 896 -1 -1 896 -1 941 -1 936 936 936 941 941 892 -1 -1 941 826 -1 896 -1 -1 936 896 -1 941 941 941 941
result:
ok 40 numbers
Test #5:
score: 0
Accepted
time: 1ms
memory: 7640kb
input:
20 40 11 3 622 15 1 463 1 13 754 10 16 87 17 16 602 19 4 1003 4 5 50 1 12 598 13 11 600 8 12 26 8 3 325 18 17 329 9 15 961 12 6 110 8 12 756 8 6 415 19 11 159 8 6 595 6 6 32 10 15 573 18 9 659 2 16 685 11 15 881 10 11 614 20 8 941 12 17 473 12 20 796 17 12 881 13 4 870 5 1 76 19 3 191 17 8 698 7 20 ...
output:
685 796 685 659 614 622 796 685 615 685 622 -1 -1 622 622 622 622 796 685 614 622 622 796 602 685 615 615 796 -1 622 796 685 659 614 685 659 614 685 685 685 685 659 -1 685 685 685 685 796 685 685 -1 685 598 796 685 796 796 622 -1 685 685 622 685 622 622 615 622 685 -1 -1 -1 685 796 615 615 796 685 7...
result:
ok 80 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 7652kb
input:
30 60 20 6 610 28 8 847 1 9 442 30 5 574 5 23 49 12 17 415 3 24 116 3 22 164 3 16 464 27 1 179 19 11 689 16 8 840 12 28 242 13 14 300 20 21 243 6 7 50 10 28 207 27 2 858 5 17 164 27 19 839 28 25 399 10 11 185 6 10 157 24 8 309 2 22 722 5 4 688 13 19 314 10 15 858 13 22 286 12 24 376 29 17 37 7 4 762...
output:
526 513 513 513 464 526 442 513 526 513 526 526 526 526 513 376 513 526 526 526 513 513 526 513 498 526 464 526 498 394 526 526 526 526 526 526 498 526 526 526 526 526 526 526 442 498 526 513 526 526 513 526 513 526 513 526 513 513 513 376 526 442 513 513 513 415 464 415 498 513 526 498 498 464 513 ...
result:
ok 120 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 9772kb
input:
40 80 12 19 34 3 23 7 2 6 779 20 14 464 14 38 950 39 1 46 14 13 18 17 16 736 4 35 816 9 13 507 27 38 18 1 3 905 29 9 628 3 32 740 37 3 410 23 33 428 19 8 69 38 34 501 35 31 446 9 7 831 34 21 33 14 7 133 9 24 545 18 9 247 20 6 10 19 24 176 4 30 780 14 15 108 1 10 263 17 38 748 38 2 894 36 28 556 2 7 ...
output:
556 545 412 -1 -1 464 545 556 464 545 464 556 -1 295 545 424 556 545 545 365 464 545 556 556 412 556 412 464 464 556 545 464 545 464 -1 556 556 464 464 424 -1 410 -1 -1 -1 556 545 295 464 545 556 412 464 545 464 545 -1 545 -1 295 545 545 464 545 -1 545 -1 556 558 410 -1 464 545 464 464 -1 -1 424 -1 ...
result:
ok 160 numbers
Test #8:
score: 0
Accepted
time: 0ms
memory: 7736kb
input:
50 100 33 19 845 50 31 685 27 25 461 15 36 18 17 35 853 49 32 539 7 6 840 24 8 1013 26 31 438 25 29 943 7 17 547 9 4 976 43 50 94 10 23 938 46 47 649 13 6 894 42 20 854 16 27 51 17 4 690 6 18 456 37 8 44 12 35 681 18 43 306 47 24 882 38 46 91 27 29 998 13 22 894 31 18 1010 42 27 165 9 2 75 6 19 588 ...
output:
512 674 -1 701 544 541 572 572 572 674 572 572 541 572 544 544 544 588 853 572 572 572 572 572 572 539 572 572 572 544 -1 572 541 572 544 572 572 541 572 674 544 588 588 544 588 544 572 853 572 507 572 674 714 674 572 714 853 732 572 732 572 512 588 541 544 572 544 714 572 572 -1 853 541 544 541 572...
result:
ok 200 numbers
Test #9:
score: -100
Time Limit Exceeded
input:
60 120 34 41 6 4 31 444 54 5 918 58 8 474 6 44 453 43 52 497 43 59 354 25 40 189 26 23 273 19 42 977 6 1 296 55 56 716 22 21 116 22 46 392 52 7 103 10 26 502 60 39 358 14 20 646 48 4 772 17 29 759 11 40 781 3 8 637 21 46 886 28 21 181 7 48 425 36 40 2 43 33 803 59 21 859 36 28 548 4 9 951 57 13 170 ...