QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#821747 | #8941. Even or Odd Spanning Tree | Suzuranovo# | RE | 1ms | 3624kb | C++23 | 3.2kb | 2024-12-19 17:48:24 | 2024-12-19 17:48:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
struct node { int v, w; };
struct DSU {
vector<int> fa;
DSU(int n) { init(n); } void init(int n) {
fa.resize(n); iota(fa.begin(), fa.end(), 0);
}
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void merge(int u, int v) { fa[find(u)] = find(v); }
bool same(int u, int v) { return find(u) == find(v); }
};
struct st {
array<vector<node>, 19> s;
vector<vector<node>> mp;
vector<int> dep;
int n;
void init(int _n) {
n = _n;
for (int i = 0; i < 19; ++i)
s[i] = vector<node>(n + 1);
mp = vector<vector<node>>(n + 1);
dep = vector<int>(n + 1); dep[1] = 1;
}
void add(int u, int v, int w) {
mp[u].push_back({ v,w });
mp[v].push_back({ u,w });
}
void build() {
dfs(1);
for (int b = 1; b <= __lg(n); ++b) {
for (int i = 1; i <= n; ++i) {
auto [v, w] = s[i][b - 1];
s[b][i] = s[b - 1][v];
s[b][i].w = max(s[b][i].w, w);
}
}
}
int query(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int res = 0;
while (dep[u] > dep[v]) {
auto [x, w] = s[__lg(dep[u] - dep[v])][u];
u = x, res = max(res, w);
}
if (u == v) return res;
for (int b = __lg(dep[u]); b >= 0; --b) {
auto [lv, lw] = s[b][u];
auto [rv, rw] = s[b][v];
if (lv == rv) continue;
res = max({ res,lw,rw });
u = lv, v = rv;
}
res = max({ res,s[0][u].w,s[0][v].w });
return res;
}
void dfs(int u) {
for (auto [v, w] : mp[u]) {
if (v == s[0][u].v) continue;
dep[v] = dep[u] + 1;
s[0][v] = { u,w }; dfs(v);
}
}
};
void solve() {
int n, m; cin >> n >> m;
// vector<vector<node>> mp(n + 1);
vector<array<int, 3>> e(m);
vector<bool> vis(m);
for (auto& [w, u, v] : e)
cin >> u >> v >> w;
sort(e.begin(), e.end());
DSU d(n + 1); ll sum = 0;
st s; s.init(n); int q = 0;
for (int i = 0; i < m; ++i) {
auto [w, u, v] = e[i];
if (d.same(u, v)) continue;
sum += w; d.merge(u, v);
vis[i] = true; ++q;
s.add(u, v, w);
}
// cerr << "buildede" << endl;
// 判断不连通
if (q < n - 1) {
cout << "-1 -1\n";
return;
}
s.build();
// cerr << "builded" << endl;
array<ll, 2> ans{ inf,inf };
ans[sum & 1] = sum;
for (int i = 0; i < m; ++i) {
if (vis[i]) continue;
auto [w, u, v] = e[i];
int mx = s.query(u, v);
ll nres = sum - mx + w;
if ((nres & 1) == (sum & 1)) continue;
ans[nres & 1] = min(ans[nres & 1], nres);
}
if (ans[0] == inf) ans[0] = -1;
if (ans[1] == inf) ans[1] = -1;
cout << ans[0] << ' ' << ans[1] << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(0);
int t; cin >> t;
while (t--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3624kb
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
Runtime Error
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...