QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#613772 | #8941. Even or Odd Spanning Tree | Ashbourne | WA | 97ms | 46780kb | C++20 | 4.1kb | 2024-10-05 14:43:23 | 2024-10-05 14:43:25 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
const int N = 4e5 + 10;
const int INF = 1e9 + 7;
#define pii pair<int,int>
using namespace std;
int n, m, tot = 0, dfn[N], depth[N], pos[N], pot[N], len[N];
vector<pii> G[N];
struct Edge{
int u, v, w;
};
int fa[N];
void dfs(int cur, int dep, int ww){
dfn[++tot] = cur;
len[tot] = ww;
depth[tot] = dep;
pos[cur] = tot;
pot[cur] = tot;
for(auto [y, w]: G[cur]){
if(!pos[y]){
dfs(y, dep + 1, w);
dfn[++tot] = cur, depth[tot] = dep;
pot[cur] = max(pot[cur], tot);
}
}
}
int st[35][N], rev[35][N], lg[N], ff[35][N][2];
void init(){
for(int i = 1; i <= tot; ++ i) st[0][i] = depth[i], rev[0][i] = dfn[i], lg[i] = ((i & (i - 1)) == 0) ? lg[i - 1] + 1 : lg[i - 1];
for(int i = 1; i <= tot; ++ i){
if(len[i] == -1) ff[0][i][0] = -1, ff[0][i][1] = -1;
else {
int x = len[i] & 1;
ff[0][i][x ^ 1] = -1;
ff[0][i][x] = len[i];
}
}
for(int i = 1; i <= lg[tot]; ++ i)
for(int j = 1; j + (1 << i) - 1 <= tot; ++ j)
if(st[i - 1][j] < st[i - 1][j + (1 << (i - 1))]){
st[i][j] = st[i - 1][j], rev[i][j] = rev[i - 1][j];
ff[i][j][0] = ff[i - 1][j][0];
ff[i][j][1] = ff[i - 1][j][1];
}
else
{
st[i][j] = st[i - 1][j + (1 << (i - 1))], rev[i][j] = rev[i - 1][j + (1 << (i - 1))];
ff[i][j][0] = max(ff[i - 1][j][0], ff[i - 1][j + (1 << (i - 1))][0]);
ff[i][j][1] = max(ff[i - 1][j][1], ff[i - 1][j + (1 << (i - 1))][1]);
}
}
int query(int l, int r){
int k = lg[r - l + 1];
return st[k][l] < st[k][r + 1 - (1 << k)] ? rev[k][l]: rev[k][r + 1 - (1 << k)];
}
pii piiquery(int l, int r){
int k = lg[r - l + 1];
// cout << r - l + 1 << endl;
// cout << k << endl;
// cout << ff[k][l][0] << " " << ff[k][r + 1 - (1 << k)][0] << endl;
return
{
max(ff[k][l][0], ff[k][r + 1 - (1 << k)][0]),
max(ff[k][l][1], ff[k][r + 1 - (1 << k)][1])
};
}
int find(int x){
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
// int vis[N];
void clear(){
lg[0] = -1;
tot = 0;
for(int i = 1; i <= n; ++ i) G[i].clear(), fa[i] = i;
for(int i = 1; i <= 2 * n; ++ i) len[i] = -1;
// for(int i = 0; i <= m; ++ i) vis[i] = 0;
}
void solve(){
cin >> n >> m;
clear();
vector<int> flag(m + 1);
vector<Edge> E;
for(int i = 1; i <= m; ++ i){
int x, y, w;
cin >> x >> y >> w;
E.push_back({x, y, w});
}
sort(E.begin(), E.end(), [&](Edge a, Edge b){
return a.w < b.w;
});
int cnt = 0, tt = 0;
vector<int> ans(2);
ans[0] = ans[1] = INF;
for(int i = 0; i < E.size(); ++ i){
int x = E[i].u, y = E[i].v, w = E[i].w;
if(cnt == n - 1) break;
int xx = find(x), yy = find(y);
if(xx != yy){
G[x].push_back({y, w});
G[y].push_back({x, w});
cnt ++;
fa[x] = yy;
flag[i] = 1;
tt += w;
}
}
dfs(1, 1, -1);
// for(int i = 1; i <= tot; ++ i) cout << dfn[i] << " " << len[i] << endl;
init();
// for(int i = 1; i <= tot; ++ i) cout << lg[i] << endl;
if(cnt != n - 1){
cout << "-1 -1" << endl;
return;
}
int xxx = tt & 1;
ans[xxx] = tt;
for(int i = 0; i < E.size(); ++ i){
if(flag[i]) continue;
int x = E[i].u, y = E[i].v, w = E[i].w;
if(pot[y] < pos[x]) swap(x, y);
// cout << pos[x] << " " << pot[y] << endl;
auto[f, s] = piiquery(pos[x], pot[y]);
// cout << f << " " << s << endl;
int ss[2]; ss[0] = f, ss[1] = s;
int xx = w & 1; xx ^= 1;
if(ss[xx] == -1) continue;
// cout << xx << endl;
// cout << << endl;
ans[xxx ^ 1] = min(ans[xxx ^ 1], ans[xxx] - ss[xx] + w);
}
cout << (ans[0] == INF? -1 : ans[0]);
cout << " " << (ans[1] == INF? -1 : ans[1]) << endl;
}
signed main(){
ios::sync_with_stdio(0);
int T; cin >> T;
while(T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28172kb
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: 97ms
memory: 46780kb
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 2528196379 1086225290 -1 1263932600 -1 1011107802 991972695 1971474182 -1 3615757924 -1 960749696 -1 -1 3323961091 1034445620 914654231 936606276 1132501971 3000802974 -1 -1 3417603403 2479987500 -1 2615323022 -1 -1 2428753593 1519735496 -1 -1 3315830557 526120556 254165229 -1 2356599637 10657381...
result:
wrong answer 1st numbers differ - expected: '3140067932', found: '-1'