QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606735 | #8941. Even or Odd Spanning Tree | Komorebie# | WA | 163ms | 3744kb | C++17 | 3.8kb | 2024-10-03 11:53:42 | 2024-10-03 11:53:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PII pair<int, ll>
const ll N = 2e5 + 10, M = 5e5 + 10, inf = 0x3f3f3f3f;
ll n, m, p[N];
struct edge {
ll a, b, w;
bool operator<(const edge& a) const { return this->w < a.w; }
};
ll find(ll x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
void solve()
{
cin >> n >> m;
vector<edge> ed(m + 1);
for (int i = 1; i <= m; i++) {
ll a, b, c;
cin >> a >> b >> c;
ed[i] = {a, b, c};
}
sort(ed.begin() + 1, ed.end());
for (int i = 1; i <= n; i++) p[i] = i;
unordered_set<ll> st;
vector<vector<PII>> G(n + 1);
ll res = 0;
for (int i = 1; i <= m; i++) {
ll x = ed[i].a, y = ed[i].b, z = ed[i].w;
if (find(x) != find(y)) {
p[find(x)] = find(y);
res += z;
st.insert(i);
}
}
if (st.size() < n - 1) {
cout << -1 << " " << -1 << endl;
return;
}
for (int i = 1; i <= m; i++) {
ll x = ed[i].a, y = ed[i].b, z = ed[i].w;
if (st.count(i)) {
G[x].push_back({y, z}), G[y].push_back({x, z});
}
}
vector<vector<int>> fa(n + 1, vector<int>(31));
vector<vector<vector<int>>> F(2, vector<vector<int>>(n + 1, vector<int>(31, inf)));
vector<int> dep(n + 1);
auto dfs = [&](auto dfs, int u, int f, int fw) -> void {
if (fw & 1) {
F[1][u][0] = fw;
}
else {
F[0][u][0] = fw;
}
fa[u][0] = f;
dep[u] = dep[f] + 1;
for (int i = 1; i < 31; i++) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
F[1][u][i] = min(F[1][fa[u][i - 1]][i - 1], F[1][u][i - 1]);
F[0][u][i] = min(F[0][fa[u][i - 1]][i - 1], F[0][u][i - 1]);
}
for (auto [v, w] : G[u]) {
if (v != f) {
dfs(dfs, v, u, w);
}
}
};
// auto lca = [&](int x, int y) {
// if (dep[x] < dep[y]) {
// swap(x, y);
// }
// int d = dep[x] - dep[y];
// for (int i = 0; (1 << i) <= d; i++) {
// if ((d >> i) & 1) x = fa[x][i];
// }
// if (x == y) return x;
// for (int i = log2(dep[y]); i >= 0; i--) {
// if (fa[x][i] != fa[y][i]) {
// x = fa[x][i];
// y = fa[y][i];
// }
// }
// return fa[x][0];
// };
auto get = [&](int x, int y, int idx) {
if (dep[x] < dep[y]) {
swap(x, y);
}
int d = dep[x] - dep[y];
int tmp = inf;
for (int i = 0; (1 << i) <= d; i++) {
if ((d >> i) & 1) {
tmp = min(tmp, F[idx][x][i]);
x = fa[x][i];
}
}
if (x == y) return tmp;
for (int i = log2(dep[y]); i >= 0; i--) {
if (fa[x][i] != fa[y][i]) {
tmp = min(tmp, F[idx][y][i]);
tmp = min(tmp, F[idx][x][i]);
x = fa[x][i];
y = fa[y][i];
}
}
return min({tmp, F[idx][x][0], F[idx][y][0]});
};
dfs(dfs, 1, 0, 0);
ll ans = 4e18;
for (int i = 1; i <= m; i++) {
if (!st.count(i)) {
auto [u, v, w] = ed[i];
int t = get(u, v, 1 - (w & 1));
if (t != inf) {
ans = min(res - t + w, ans);
}
}
}
if (ans == 4e18) ans = -1;
if (res & 1) {
cout << ans << " " << res << endl;
}
else {
cout << res << " " << ans << endl;
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;
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: 3684kb
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: 163ms
memory: 3744kb
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:
3140067932 3159441841 1262790434 1332462897 1263932600 1395151561 1189242112 1180186165 2248358640 2277656157 3776889482 3738936375 1093500444 1058677117 3444549292 3402127725 1258609116 1187873655 1395482806 1411327105 3456885934 3486092007 3943951826 3988876469 2479987500 2733874051 2909126794 317...
result:
wrong answer 4th numbers differ - expected: '1309454727', found: '1332462897'