QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#610604#8941. Even or Odd Spanning Treeucup-team4975#WA 127ms26540kbC++233.4kb2024-10-04 16:35:592024-10-04 16:35:59

Judging History

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

  • [2024-10-04 16:35:59]
  • 评测
  • 测评结果:WA
  • 用时:127ms
  • 内存:26540kb
  • [2024-10-04 16:35:59]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;

const int N=500010;
const int inf=1000000000;
typedef long long ll;
int n,m,istree[N],f[N][20],g[N][20],_fa[N][20],llg[N]; //f odd edge  g even edge
//unordered_map<ll,int>id;

inline ll _(int x,int y){return 1ll*(1ll*x-1)*n+y;}
struct disunion{
	int fa[N];
	inline void init(int x){for(int i=1;i<=x;i++)fa[i]=i;}
	inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
}t;

struct Edge{
	int x,y,z;
	inline bool operator<(const Edge that){
		return z<that.z;
	}
}a[N];

struct Graph{
	int tot,head[N],suiv[N],edge[N],ver[N],dep[N];

	inline void init(int x)
	{
		for(int i=1;i<=x;i++)
			head[i]=0;
		tot=0;
	}

	inline void lnk(int x,int y,int z)
	{
		ver[++tot]=y;
		edge[tot]=z;
		suiv[tot]=head[x];
		head[x]=tot;
	}

	inline void dfs(int x,int fa=-1)
	{
		for(int i=1;(1<<i)<=dep[x];i++)
			_fa[x][i]=_fa[_fa[x][i-1]][i-1],
			f[x][i]=max(f[x][i-1],f[_fa[x][i-1]][i-1]),
			g[x][i]=max(g[x][i-1],g[_fa[x][i-1]][i-1]);
		for(int i=head[x];i;i=suiv[i])
		{
			int y=ver[i],z=edge[i];
			if(y==fa)continue;
			if(z&1)f[y][0]=z,g[y][0]=-inf;
			else g[y][0]=z,f[y][0]=-inf;
			_fa[y][0]=x;
			dep[y]=dep[x]+1;
			dfs(y,x);
		}
	}

	inline int query(int x,int y,int op)   //op==0 query odd(f) op==1 query even(g)
	{
		int res=-inf;
		if(dep[x]<dep[y])swap(x,y);
		while(dep[x]>dep[y])
		{
			if(!op)res=max(res,f[x][llg[dep[x]-dep[y]]]);
			else res=max(res,g[x][llg[dep[x]-dep[y]]]);
			x=_fa[x][llg[dep[x]-dep[y]]];
		}
		if(x==y)return res;
		for(int i=llg[dep[x]];i>=0;i--)
			if(_fa[x][i]!=_fa[y][i])
			{
				if(!op)res=max({res,f[x][i],f[y][i]});
				else res=max({res,g[x][i],g[y][i]});
				x=_fa[x][i];
				y=_fa[y][i];
			}
		if(!op)return max({res,f[x][0],f[y][0]});
		else return max({res,g[x][0],g[y][0]});
	}
}e;
			

signed main()
{
	for(int i=1;i<N;i++)
		llg[i]=log2(i);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		//id.clear();	
		ll evenans=0,oddans=0;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
		sort(a+1,a+1+m);
		t.init(n);
		int cnt=0;
		for(int i=1;i<=m;i++)
		{
			int fx=t.find(a[i].x),fy=t.find(a[i].y);
			if(fx==fy)continue;
			t.fa[fx]=fy;
			istree[i]=1;
			evenans+=1ll*a[i].z;
			//id[_(a[i].x,a[i].y)]=id[_(a[i].y,a[i].x)]=i;
			e.lnk(a[i].x,a[i].y,a[i].z);
			e.lnk(a[i].y,a[i].x,a[i].z);
			cnt++;
		}
		if(cnt<n-1)
		{
			puts("-1 -1");
			for(int i=1;i<=m;i++)
				istree[i]=0;
			e.init(n);
			continue;
		}
		for(int i=1;i<=n;i++)
			for(int j=0;j<20;j++)
				f[i][j]=g[i][j]=-inf;
		_fa[1][0]=0;
		e.dep[1]=1;
		e.dfs(1);
		
		//while(1)
		//{
		//	int x,y,z;
		//	cin>>x>>y>>z;
		//	cout<<e.query(x,y,z)<<endl;
		//}
		//cout<<evenans<<endl;
		ll add=inf;
		for(int i=1,treeedge;i<=m;i++)
		{
			int res=-inf;
			if(istree[i])continue;
			/*
			if((treeedge=id[_(a[i].x,a[i].y)]))
			{
				if((a[treeedge].z%2)==(a[i].z%2))continue;
				add=min(add,1ll*abs(1ll*a[treeedge].z-a[i].z));
				continue;
			}*/
			if(a[i].z&1)//odd
				res=e.query(a[i].x,a[i].y,1);
			else res=e.query(a[i].x,a[i].y,0);
			if(res!=-inf)
				add=min(add,abs(res-1ll*a[i].z));
		}
		//cout<<"!"<<endl;
		if(add!=inf)oddans=evenans+add;
		else oddans=-1;
		if(evenans&1)swap(evenans,oddans);
		printf("%lld %lld\n",evenans,oddans);
		for(int i=1;i<=m;i++)
			istree[i]=0;
		e.init(n);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 24464kb

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: 0
Accepted
time: 95ms
memory: 24488kb

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 2535532661
2909126794 293...

result:

ok 20000 numbers

Test #3:

score: -100
Wrong Answer
time: 127ms
memory: 26540kb

input:

100
1806 5000
1 2 139994119
2 3 184924112
3 4 670813536
4 5 443325848
5 6 767909046
6 7 531742851
7 8 364385440
8 9 918195219
9 10 731896671
10 11 671688362
11 12 558665746
12 13 154497842
13 14 28636459
14 15 570343686
15 16 419626123
16 17 817852951
17 18 517701907
18 19 952451718
19 20 141747977
...

output:

380509244078 380509217939
236564714828 236564588423
317690341848 317690193297
416922743878 416923542879
411997033392 411996924725
231780454372 231780820627
156856996316 156856345151
239278757000 239278493321
288480848080 288481502909
347301406524 347301269265
362642903994 362643090967
158361335208 1...

result:

wrong answer 9th numbers differ - expected: '411997066938', found: '411997033392'