QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606578 | #8941. Even or Odd Spanning Tree | liguo# | WA | 93ms | 16212kb | C++20 | 2.4kb | 2024-10-03 10:44:11 | 2024-10-03 10:44:11 |
Judging History
answer
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=1e6+10;
const int mod=1e9+7;
long long read(){
long long ans=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
return ans*f;
}
int n,m,z,t,fa[maxn];
char s[maxn];
struct node{
int x,y,z;
int next,to;
}e[maxn*2],h[maxn];
bool cmp(node a,node b){
return a.z<b.z;
}
int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
int f[maxn][21],head[maxn],k=0;
void add(int x,int y,int z){
e[++k].next=head[x];
e[k].to=y;
e[k].z=z;
head[x]=k;
}
int deep[maxn],bk[maxn],fz[maxn][21];
void dfs(int id,int ff){
deep[id]=deep[ff]+1;
f[id][0]=ff;
for(int i=1;i<20;i++){
if(f[id][i-1]==0) break;
f[id][i]=f[f[id][i-1]][i-1];
fz[id][i]=max(fz[f[id][i-1]][i-1],fz[id][i-1]);
}
for(int i=head[id];i;i=e[i].next){
int tp=e[i].to;
if(tp==ff) continue;
fz[tp][0]=e[i].z;
dfs(tp,id);
}
}
int lca(int a,int b){
if(deep[a]>deep[b]) return lca(b,a);
int tmp=0;
for(int i=19;i>=0;i--){
if(deep[f[b][i]]<deep[a]) continue;
tmp=max(tmp,fz[b][i]);
b=f[b][i];
}
if(b==a) return tmp;
for(int i=19;i>=0;i--){
if(f[b][i]==f[a][i]) continue;
tmp=max(tmp,fz[b][i]);
tmp=max(tmp,fz[a][i]);
b=f[b][i];
a=f[a][i];
}
tmp=max(tmp,fz[a][0]);
tmp=max(tmp,fz[b][0]);
return tmp;
}
int main(){
t=read();
while(t--){
n=read();m=read();
for(int i=1;i<=m;i++){
h[i].x=read();
h[i].y=read();
h[i].z=read();
bk[i]=0;
}
sort(h+1,h+m+1,cmp);
for(int i=1;i<=n;i++){
fa[i]=i;
head[i]=0;
}
k=0;
long long sm=0,ans=-1;
int cnt=n-1;
for(int i=1;i<=m;i++){
int a=find(h[i].x);
int b=find(h[i].y);
if(a==b) continue;
bk[i]=1;
fa[a]=b;
sm+=h[i].z;
add(h[i].x,h[i].y,h[i].z);
add(h[i].y,h[i].x,h[i].z);
cnt--;
if(!cnt) break;
}
if(cnt>0){
printf("-1 -1\n");
continue;
}
dfs(1,0);
for(int i=1;i<=m;i++){
if(bk[i]) continue;
int c=lca(h[i].x,h[i].y);
long long tmp=sm+h[i].z-c;
if((tmp+sm)%2==0) continue;
if(ans>=0) ans=min(ans,tmp);
else ans=tmp;
}
if(sm%2) printf("%lld %lld\n",ans,sm);
else printf("%lld %lld\n",sm,ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16212kb
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: 93ms
memory: 16164kb
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 3191118501 1262790434 955175917 1263932600 1307926149 1139715652 1180186165 2248358640 2261370885 3776889482 3738936375 1093500444 1058677117 3433711014 3402127725 1102306984 1187873655 1395482806 1348589041 3456885934 3486092007 3943951826 3949300341 2479987500 2535532661 2909126794 2791...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '3191118501'