QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#538278 | #8941. Even or Odd Spanning Tree | ucup-team4736# | WA | 122ms | 101908kb | C++14 | 3.0kb | 2024-08-31 09:55:02 | 2024-08-31 09:55:02 |
Judging History
answer
// Problem: J. Even or Odd Spanning Tree
// Platform: undefined - The 3rd Universal Cup. Stage 8: Cangqian
// URL: https://contest.ucup.ac/contest/1780/problem/8941
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// Author:British Union
// Long live UOB and koala
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5e5+5,INF=1e18;
int T,n,m;
struct edge{
int u,v,w;
}E[maxn];
bool operator <(edge A,edge B){return A.w<B.w;}
struct LCT{
vector<pair<int,int>> e[maxn];
int fa[maxn],sze[maxn],son[maxn],dep[maxn],seg[maxn],to[maxn],rev[maxn],W[maxn];
int st[maxn][19];
int Max(int l,int r){
if(l>r)return 0;
int L=__lg(r-l+1);
return max(st[l][L],st[r-(1<<L)+1][L]);
}
void dfs1(int u,int f){
fa[u]=f,dep[u]=dep[f]+1,sze[u]=1;
for(auto [v,w]:e[u]){
if(v==f)continue;
dfs1(v,u);
sze[u]+=sze[v];W[v]=w;
if(sze[v]>sze[son[u]])son[u]=v;
}
}
void dfs2(int u,int t){
to[u]=t,seg[u]=++seg[0],rev[seg[0]]=u;
if(son[u])dfs2(son[u],t);
for(auto [v,w]:e[u])if(v!=son[u]&&v!=fa[u])dfs2(v,v);
}
void predo(){
dfs1(1,0);dfs2(1,1);
for(int i=1;i<=n;i++)st[seg[i]][0]=W[i];
for(int j=1;j<=18;j++)for(int i=1;i<=n;i++)st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
int task(int x,int y){
int res=0;
while(to[x]!=to[y]){
if(dep[to[x]]<dep[to[y]])swap(x,y);
res=max(res,Max(seg[to[x]],seg[x]));
x=fa[to[x]];
}
if(dep[x]>dep[y])swap(x,y);
res=max(res,Max(seg[x]+1,seg[y]));
return res;
}
void clear(){
for(int i=1;i<=n;i++){
son[i]=0;
for(int j=0;j<19;j++)st[i][j]=0;
e[i].clear();
}
}
}Even,Odd;
int fa[maxn],tp[maxn];
void clear(){
for(int i=1;i<=m;i++)tp[i]=0;
Even.clear(),Odd.clear();
}
int Find(int x){
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
cin>>n>>m;
for(int i=1;i<=m;i++)cin>>E[i].u>>E[i].v>>E[i].w;
sort(E+1,E+m+1);
for(int i=1;i<=n;i++)fa[i]=i;
int W=0,qsy=0,cnt=0;
for(int i=1;i<=m;i++){
if(Find(E[i].u)==Find(E[i].v))continue;
fa[Find(E[i].u)]=Find(E[i].v);
tp[i]=1;W+=E[i].w,qsy+=(E[i].w&1);++cnt;
if(E[i].w&1)Even.e[E[i].u].push_back({E[i].v,E[i].w}),Even.e[E[i].v].push_back({E[i].u,E[i].w}),Odd.e[E[i].u].push_back({E[i].v,0}),Odd.e[E[i].v].push_back({E[i].u,0});
else Odd.e[E[i].u].push_back({E[i].v,E[i].w}),Odd.e[E[i].v].push_back({E[i].u,E[i].w}),Even.e[E[i].u].push_back({E[i].v,0}),Even.e[E[i].v].push_back({E[i].u,0});
}
if(cnt<n-1){cout<<-1<<" "<<-1<<endl;clear();continue;}
int W2=INF;
Even.predo(),Odd.predo();
for(int i=1;i<=m;i++){
if(tp[i]==1)continue;
if(E[i].w&1){
int mx=Odd.task(E[i].u,E[i].v);
if(mx==0)continue;
W2=min(W2,W-mx+E[i].w);
}else{
int mx=Even.task(E[i].u,E[i].v);
if(mx==0)continue;
W2=min(W2,W-mx+E[i].w);
}
}
if(W2==INF)W2=-1;
if(W&1)cout<<W2<<" "<<W<<endl;
else cout<<W<<" "<<W2<<endl;
clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 56936kb
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: 122ms
memory: 101908kb
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 1320272763 1263932600 1333657263 1189242112 1180186165 2248358640 2261370885 4093385374 3738936375 1207733704 1058677117 3444549292 3402127725 1201099898 1187873655 1395482806 1440596255 3456885934 3486092007 3943951826 3988876469 2479987500 2752449745 2909126794 293...
result:
wrong answer 4th numbers differ - expected: '1309454727', found: '1320272763'