QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#548141 | #8941. Even or Odd Spanning Tree | ucup-team004# | WA | 96ms | 3788kb | C++20 | 3.5kb | 2024-09-05 15:51:22 | 2024-09-05 15:51:23 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
struct DSU {
std::vector<int> f, siz;
DSU() {}
DSU(int n) {
init(n);
}
void init(int n) {
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<std::array<int, 3>> e(m);
for (int i = 0; i < m; i++) {
int u, v, w;
std::cin >> u >> v >> w;
u--;
v--;
e[i] = {w, u, v};
}
std::sort(e.begin(), e.end());
i64 ans = 0;
int comp = n;
DSU dsu(n);
std::vector<std::vector<std::array<int, 2>>> adj(n);
for (auto [w, u, v] : e) {
if (dsu.merge(u, v)) {
ans += w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
comp--;
}
}
if (comp > 1) {
std::cout << -1 << " " << -1 << "\n";
return;
}
i64 out[2] {-1, -1};
out[ans % 2] = ans;
const int logn = std::__lg(n);
std::vector p(logn + 1, std::vector<int>(n, -1));
std::vector mx(logn + 1, std::vector(n, std::array<int, 2>{-1, -1}));
std::vector<int> dep(n);
auto dfs = [&](auto &self, int x) -> void {
for (int i = 0; (2 << i) <= dep[x]; i++) {
p[i + 1][x] = p[i][p[i][x]];
mx[i + 1][x][0] = std::max(mx[i][x][0], mx[i][p[i][x]][0]);
mx[i + 1][x][1] = std::max(mx[i][x][1], mx[i][p[i][x]][1]);
}
for (auto [y, w] : adj[x]) {
if (y == p[0][x]) {
continue;
}
p[0][y] = x;
mx[0][y][w % 2] = w;
self(self, y);
}
};
dfs(dfs, 0);
auto query = [&](int x, int y, int par) {
if (dep[x] < dep[y]) {
std::swap(x, y);
}
int res = -1;
for (int i = logn; i >= 0; i--) {
if (dep[x] - (1 << i) >= dep[y]) {
res = std::max(res, mx[i][x][par]);
x = p[i][x];
}
}
if (x == y) {
return res;
}
for (int i = logn; i >= 0; i--) {
if (p[i][x] != p[i][y]) {
res = std::max({res, mx[i][x][par], mx[i][y][par]});
x = p[i][x];
y = p[i][y];
}
}
return std::max({res, mx[0][x][par], mx[0][y][par]});
};
for (auto [w, u, v] : e) {
int x = query(u, v, (w + 1) % 2);
if (x != -1) {
i64 s = ans - x + w;
out[s % 2] = std::max(out[s % 2], s);
}
}
std::cout << out[0] << " " << out[1] << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3532kb
input:
3 2 1 1 2 5 3 1 1 3 1 4 4 1 2 1 1 3 1 1 4 1 2 4 2
output:
-1 5 -1 -1 4 3
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 96ms
memory: 3788kb
input:
10000 16 50 1 2 649251632 2 3 605963017 3 4 897721528 4 5 82446151 5 6 917109168 6 7 79647183 7 8 278566178 7 9 573504723 3 10 679859251 8 11 563113083 1 12 843328546 10 13 594049160 11 14 997882839 7 15 569431750 2 16 244494799 16 7 960172373 13 4 317888322 13 3 446883598 9 3 678142319 5 8 43208692...
output:
3840110198 3941554631 1376467166 2124445815 1849365886 2177809565 2144862466 1338409779 2682313660 3072149007 4562589320 4187297631 2011213264 1404129483 4168980718 4200815683 2081445350 1993296867 2293139106 2324672773 4030236580 4350124765 4778954032 4702420591 3462091252 3355315679 3596304406 388...
result:
wrong answer 1st numbers differ - expected: '3140067932', found: '3840110198'