QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#539468 | #8941. Even or Odd Spanning Tree | ucup-team052# | ML | 0ms | 17976kb | C++23 | 2.6kb | 2024-08-31 14:55:04 | 2024-08-31 14:55:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
typedef long long ll;
const int N = 5e5 + 5, INF = 2e9;
vector <pii> adj[N];
int f[N], u[N], v[N], w[N], id[N], used[N], fa[N / 2][20], mx[N / 2][20][2], dep[N], ww[N];
int T, n, m;
bool cmp(int i, int j) {
return w[i] < w[j];
}
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void dfs1(int u) {
mx[u][0][0] = mx[u][0][1] = 0;
mx[u][0][ww[u] % 2] = ww[u];
for (int i = 1; i <= 17; i++) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int j = 0; j <= 1; j++) {
mx[u][i][j] = max(mx[u][i - 1][j], mx[fa[u][i - 1]][i - 1][j]);
}
}
for (auto v : adj[u]) {
if (v.first == fa[u][0]) continue;
fa[v.first][0] = u; ww[v.first] = v.second; dep[v.first] = dep[u] + 1; dfs1(v.first);
}
}
int jump(int u, int k) {
for (int i = 0; k; i++) {
if ((k >> i) & 1) {
u = fa[u][k];
k ^= (1 << i);
}
}
return u;
}
int query(int u, int k, int o) {
int ans = 0;
for (int i = 0; k; i++) {
if ((k >> i) & 1) {
ans = max(ans, mx[u][k][o]);
u = fa[u][k];
k ^= (1 << i);
}
}
return ans;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
u = jump(u, dep[u] - dep[v]);
for (int i = 17; i >= 0; i--) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int qry(int u, int v, int o) {
int d = lca(u, v);
return max(query(u, dep[u] - dep[d], o), query(v, dep[v] - dep[d], o));
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> T;
while (T--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= n; i++) adj[i].clear();
for (int i = 1; i <= m; i++) {
cin >> u[i] >> v[i] >> w[i];
id[i] = i;
}
sort(id + 1, id + m + 1, cmp);
ll sum = 0;
int mcnt = 0;
for (int i = 1; i <= m; i++) {
int x = find(u[id[i]]), y = find(v[id[i]]);
if (x == y) used[id[i]] = 0;
else {
++mcnt;
adj[u[id[i]]].emplace_back(v[i], w[id[i]]);
adj[v[id[i]]].emplace_back(u[i], w[id[i]]);
f[x] = y;
used[id[i]] = 1;
sum += w[id[i]];
}
}
if (mcnt != n - 1) {
cout << -1 << ' ' << -1 << '\n';
continue;
}
dfs1(1);
int ans = INF;
for (int i = 1; i <= m; i++) {
if (!used[i]) {
int res = query(u[i], v[i], (w[i] % 2) ^ 1);
if (res) ans = min(ans, w[i] - res);
}
}
ll ans0 = -1, ans1 = -1;
if (sum % 2 == 0) {
ans0 = sum;
if (ans != INF) ans1 = sum + ans;
} else {
ans1 = sum;
if (ans != INF) ans0 = sum + ans;
}
cout << ans0 << ' ' << ans1 << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 17976kb
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
Memory Limit Exceeded
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...