QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556681#8941. Even or Odd Spanning TreemayunfeiWA 102ms20356kbC++172.4kb2024-09-10 20:08:452024-09-10 20:08:46

Judging History

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

  • [2024-09-10 20:08:46]
  • 评测
  • 测评结果:WA
  • 用时:102ms
  • 内存:20356kb
  • [2024-09-10 20:08:45]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lll __int128
#define pc __builtin_popcount
#define pr pair<int,int>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll rint(ll l,ll r){return uniform_int_distribution<ll>(l,r)(rnd);}
const int maxn=5e5+10;
int T,n,m,ans,cnt,bcj[maxn],dep[maxn],f[maxn][20],fe[maxn][20][2];
vector<pr> e[maxn];
bool use[maxn];
ll sum;
int root(int x){return (x==bcj[x])?x:(bcj[x]=root(bcj[x]));}
struct edge
{
	int x,y,z;
	edge(){}
	edge(int X,int Y,int Z){x=X,y=Y,z=Z;}
}a[maxn];
bool operator < (edge X,edge Y)
{
	return X.z<Y.z;
}
void dfs(int now,int fa)
{
	dep[now]=dep[fa]+1;
	for(int i=1;i<20;i++)
	{
		f[now][i]=f[f[now][i-1]][i-1];
		fe[now][i][0]=min(fe[now][i-1][0],fe[f[now][i-1]][i-1][0]);
		fe[now][i][1]=min(fe[now][i-1][1],fe[f[now][i-1]][i-1][1]);
	}
	for(pr i:e[now]) if(i.x!=fa) f[i.x][0]=now,fe[i.x][0][i.y&1]=i.y,dfs(i.x,now);
}
int calc(int x,int y,int typ)
{
	int res=2e9;
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=19;i>=0;i--)
		if(dep[f[x][i]]>=dep[y])
			res=min(res,fe[x][i][typ]),x=f[x][i];
	if(x==y) return res;
	for(int i=19;i>=0;i--)
		if(f[x][i]!=f[y][i])
			res=min({res,fe[x][i][typ],fe[y][i][typ]}),x=f[x][i],y=f[y][i];
	return min({res,fe[x][0][typ],fe[y][0][typ]});
}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%d",&T);
	for(int i=0;i<20;i++) fe[0][i][0]=fe[0][i][1]=2e9;
	while(T--)
	{
		scanf("%d%d",&n,&m),sum=0,cnt=0,ans=2e9;
		for(int i=1;i<=n;i++)
		{
			bcj[i]=i,e[i].clear();
			for(int j=0;j<20;j++) fe[i][j][0]=fe[i][j][1]=2e9;
		}
		for(int i=1;i<=m;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z),use[i]=0;
		sort(a+1,a+m+1);
		for(int i=1;i<=m;i++)
		{
			if(root(a[i].x)!=root(a[i].y))
			{
				cnt++,use[i]=1,bcj[root(a[i].x)]=root(a[i].y),sum+=a[i].z;
				e[a[i].x].pb(mp(a[i].y,a[i].z)),e[a[i].y].pb(mp(a[i].x,a[i].z));
			}
		}
		if(cnt<n-1)
		{
			printf("-1 -1\n");
			continue;
		}
		dfs(1,0);
		for(int i=1;i<=m;i++)
		{
			if(!use[i])
			{
				int tmp=calc(a[i].x,a[i].y,(a[i].z&1)^1);
				if(tmp<=1e9) ans=min(ans,a[i].z-tmp);
			}
		}
		if(sum&1) printf("%lld %lld\n",(ans<=1e9)?sum+ans:-1,sum);
		else printf("%lld %lld\n",sum,(ans<=1e9)?sum+ans:-1);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 102ms
memory: 20356kb

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 1332462897
1263932600 1395151561
1189242112 1180186165
2248358640 2277656157
3776889482 3738936375
1093500444 1058677117
3444549292 3402127725
1258609116 1187873655
1395482806 1411327105
3456885934 3486092007
3943951826 3988876469
2479987500 2733874051
2909126794 317...

result:

wrong answer 4th numbers differ - expected: '1309454727', found: '1332462897'