QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#540749#8941. Even or Odd Spanning Treeucup-team4717#WA 68ms30464kbC++142.5kb2024-08-31 17:48:342024-08-31 17:48:36

Judging History

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

  • [2024-08-31 17:48:36]
  • 评测
  • 测评结果:WA
  • 用时:68ms
  • 内存:30464kb
  • [2024-08-31 17:48:34]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int T,n,m;

struct Node{
	int u,v,w;
}e[N];

int t[N],val[N],f[N],sz[N],cnt,sum,idt;
inline int Find(int x){
	return x==f[x]?x:Find(f[x]);
}

vector<pair<int,int> > to[N];

bool cmp(Node a,Node b){
	return a.w<b.w;
}

void merge(int x,int y,int X,int Y,int w){
	to[X].emplace_back(Y,w);
	to[Y].emplace_back(X,w);
	if(sz[x]<sz[y]) f[x]=y,sz[y]+=sz[x];
	else f[y]=x,sz[x]+=sz[y];
	cnt++;
	sum+=w;
}

int fa[N],id[N],siz[N],idx;

void dfs(int u,int f){
	fa[u]=f,id[u]=++idx,siz[u]=1;
	for(pair<int,int> pl:to[u]){
		int v=pl.first,w=pl.second;
		if(v==f) continue;
		val[v]=w;
		dfs(v,u);
		siz[u]+=siz[v];
	}
}

int ff[N];
int FFind(int x){
	return x==ff[x]?x:ff[x]=FFind(ff[x]);
}

int mn,wdf,sb,s1,s2;
bool flg=0;

void check(int x,int y){
	if(id[s1]>=id[x]&&id[s1]<=id[x]+siz[x]-1&&id[s2]>=id[x]&&id[s2]<=id[x]+siz[x]-1) return;
	if(!x) return;
	if((val[x]&1)^(sb&1)){
		mn=max(mn,val[x]),flg=1;
		if(ff[x]==FFind(x)) ff[x]=FFind(fa[x]);
	}
	check(fa[FFind(x)],y);
}

/*
1
5 10
1 5 9
3 2 2
1 3 7
4 5 5
3 2 7
1 1 5
3 2 4
5 2 8
3 1 5
1 2 3
*/

signed main(){	
	// freopen("j.in","r",stdin);
	// freopen("test.out","w",stdout);
	T=read();
	while(T--){
		n=read(),m=read(),cnt=sum=idt=idx=0;
		for(int i=1;i<=m;i++){
			e[i].u=read();
			e[i].v=read();
			e[i].w=read();
		}
		sort(e+1,e+1+m,cmp);
		for(int i=1;i<=n;i++) f[i]=i,sz[i]=1;
		for(int i=1;i<=m;i++){
			int x=Find(e[i].u),y=Find(e[i].v);
			if(x==y) continue;
			merge(x,y,e[i].u,e[i].v,e[i].w);
			
		}
		dfs(1,0);
		// cout<<cnt<<"\n";
		if(cnt<n-1){
			printf("-1 -1\n");
			for(int i=1;i<=n;i++) t[i]=val[i]=0,to[i].clear();
			continue;
		}
		int ans1=sum,ans2=1e16;idt=0;
		for(int i=1;i<=n;i++) f[i]=i,sz[i]=1,to[i].clear(),ff[i]=i;
		for(int i=1;i<=m;i++){
			int x=Find(e[i].u),y=Find(e[i].v);
			if(x==y){
				mn=0;
				sb=e[i].w,flg=0;
				s1=e[i].u,s2=e[i].v;
				check(e[i].u,1);
				check(e[i].v,1);

				if(flg) ans2=min(ans2,ans1-mn+e[i].w);
				continue;
			}
			merge(x,y,e[i].u,e[i].v,e[i].w);		
		}
		if(ans2==1e16) ans2=-1;
		if(ans1&1) swap(ans1,ans2);
		printf("%lld %lld\n",ans1,ans2);
		for(int i=1;i<=n;i++) t[i]=val[i]=0,to[i].clear();
	}
	return 0;
}
/*
1
3 4
2 1 5
2 1 0
3 1 2
2 3 9
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 30460kb

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: 68ms
memory: 30464kb

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 1309454727
1263932600 1307926149
1189242112 1180186165
2248358640 2261370885
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3988876469
2479987500 2733678685
2909126794 293...

result:

wrong answer 26th numbers differ - expected: '2535532661', found: '2733678685'