QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#789199 | #8941. Even or Odd Spanning Tree | _fcy_# | WA | 78ms | 12160kb | C++20 | 3.3kb | 2024-11-27 19:30:58 | 2024-11-27 19:30:58 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200010;
const int M = 500010;
const int inf = 1e9+10;
const ll infl = 1e15;
int n, m, fa_union[N], use[M];
array<int,3> e[M];
vector<pair<int,int>> G[N];
int find(int u){ return fa_union[u]==u? u: fa_union[u] = find(fa_union[u]); }
int fa[N][20], mine[N][20][2], dep[N];
void dfs(int u, int pre, int pre_w){
fa[u][0] = pre;
for(int i = 1; i < 20; i++)
fa[u][i] = fa[fa[u][i-1]][i-1];
mine[u][0][pre_w&1] = pre_w;
mine[u][0][pre_w&1^1] = inf;
dep[u] = dep[pre] + 1;
for(int i = 1; i < 20; i++){
mine[u][i][0] = min(mine[u][i-1][0], mine[fa[u][i-1]][i-1][0]);
mine[u][i][1] = min(mine[u][i-1][1], mine[fa[u][i-1]][i-1][1]);
}
for(auto v: G[u]){
if(v.first == pre) continue;
dfs(v.first, u, v.second);
}
return;
}
int get_lca(int u, int v, array<int,2>& mine_res){
if(dep[u] > dep[v]) swap(u, v);
mine_res = {inf, inf};
for(int i = 19; i >= 0; i--)
if(dep[fa[v][i]] >= dep[u]){
mine_res[0] = min(mine_res[0], mine[v][i][0]);
mine_res[1] = min(mine_res[1], mine[v][i][1]);
v = fa[v][i];
}
if(v == u)
return u;
for(int i = 19; i > 0; i--)
if(fa[v][i] != fa[u][i]){
mine_res[0] = min(mine_res[0], min(mine[v][i][0], mine[u][i][0]));
mine_res[1] = min(mine_res[1], min(mine[v][i][1], mine[u][i][1]));
u = fa[u][i], v = fa[v][i];
}
mine_res[0] = min(mine_res[0], min(mine[v][0][0], mine[u][0][0]));
mine_res[1] = min(mine_res[1], min(mine[v][0][1], mine[u][0][1]));
u = fa[u][0], v = fa[v][0];
return u;
}
ll sum = 0, ans[2];
void solve(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
G[i].clear();
fa_union[i] = i;
}
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &e[i][0], &e[i][1], &e[i][2]);
use[i] = 0;
}
sort(e+1, e+m+1, [&](auto u, auto v){
return u[2] < v[2];
});
sum = 0; ans[0] = ans[1] = infl;
for(int i = 1; i <= n; i++){
int fa1 = find(e[i][0]);
int fa2 = find(e[i][1]);
if(fa1 != fa2){
// printf("choose %d\n", i);
use[i] = 1;
fa_union[fa1] = fa2;
G[e[i][0]].push_back(make_pair(e[i][1], e[i][2]));
G[e[i][1]].push_back(make_pair(e[i][0], e[i][2]));
sum += e[i][2];
}
}
for(int i = 1; i <= n; i++)
if(find(i) != find(1)){
printf("-1 -1\n");
return;
}
ans[sum&1] = sum;
for(int i = 0; i < 20; i++)
mine[0][i][0] = mine[0][i][1] = inf;
dfs(1, 0, inf);
for(int i = 1; i <= m; i++)
if(!use[i]){
array<int,2> t;
int lca = get_lca(e[i][0], e[i][1], t);
if(t[0] < inf) ans[(sum+e[i][2]-t[0])&1] = min(ans[(sum+e[i][2]-t[0])&1], sum+e[i][2]-t[0]);
if(t[1] < inf) ans[(sum+e[i][2]-t[1])&1] = min(ans[(sum+e[i][2]-t[1])&1], sum+e[i][2]-t[1]);
// printf("%d %d %d\n", i, t[0], t[1]);
}
printf("%lld %lld\n", ans[0]==infl?-1ll:ans[0], ans[1]==infl?-1ll:ans[1]);
return;
}
int main(){
int t; scanf("%d", &t);
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 10040kb
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: 78ms
memory: 12160kb
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:
-1 -1 1262790434 1319989017 1263932600 1395151561 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2479987500 2733874051 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3874271354 3690727057 379770804 463569469 -1 -1 ...
result:
wrong answer 1st numbers differ - expected: '3140067932', found: '-1'