QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606599#8941. Even or Odd Spanning Treeliguo#WA 92ms16096kbC++202.7kb2024-10-03 10:57:582024-10-03 10:57:58

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 10:57:58]
  • 评测
  • 测评结果:WA
  • 用时:92ms
  • 内存:16096kb
  • [2024-10-03 10:57:58]
  • 提交

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][2];
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][1]=max(fz[f[id][i-1]][i-1][1],fz[id][i-1][1]);
		fz[id][i][0]=max(fz[f[id][i-1]][i-1][0],fz[id][i-1][0]);
	}
	for(int i=head[id];i;i=e[i].next){
		int tp=e[i].to;
		if(tp==ff) continue;
		if(e[i].z%2){
			fz[tp][0][1]=e[i].z;
			fz[tp][0][0]=0;
		}
		else{
			fz[tp][0][0]=e[i].z;
			fz[tp][0][1]=0;
		}
		dfs(tp,id);
	}
}
int lca(int a,int b,int op){
	if(deep[a]>deep[b]) return lca(b,a,op);
	int tmp=0;
	for(int i=19;i>=0;i--){
		if(deep[f[b][i]]<deep[a]) continue;
		tmp=max(tmp,fz[b][i][op]);
		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][op]);
		tmp=max(tmp,fz[a][i][op]);
		b=f[b][i];
		a=f[a][i];
	}
	tmp=max(tmp,fz[a][0][op]);
	tmp=max(tmp,fz[b][0][op]);
	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,c;
		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;
			if(h[i].z%2) c=lca(h[i].x,h[i].y,0);
			else c=lca(h[i].x,h[i].y,1);
			//printf("%d %d\n",lca(h[i].x,h[i].y,0),lca(h[i].x,h[i].y,1));
			if(c==0) continue;
			
			long long tmp=sm+h[i].z-c;
			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: 16036kb

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: 92ms
memory: 16096kb

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 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 4th numbers differ - expected: '1309454727', found: '955175917'