QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606739#8941. Even or Odd Spanning TreeUESTC_NLNS#WA 105ms16956kbC++142.2kb2024-10-03 11:55:242024-10-03 11:55:24

Judging History

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

  • [2024-10-03 11:55:24]
  • 评测
  • 测评结果:WA
  • 用时:105ms
  • 内存:16956kb
  • [2024-10-03 11:55:24]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define ll long long
using namespace std;
const int N=2e5+5,M=5e5+5,INF=0x3f3f3f3f;
const ll IINF=0x3f3f3f3f3f3f3f3f;
struct node{
	int u,v,w;
	bool operator < (const node &x) const{
		return w<x.w;
	}
}edg[M];
int T,n,m,a,b,c,dep[N],f[N],fa[N][20],mx[2][N][20];
ll tmp,ans[2];
vector<pii > e[N];
bool vis[N];
int getfa(int x) {return f[x]==x?x:(f[x]=getfa(f[x]));}
int merge(int x,int y,int w)
{
	int fx=getfa(x),fy=getfa(y);
	if(fx==fy) return 0;
	e[x].push_back({y,w});
	e[y].push_back({x,w});
	f[fy]=fx;
	tmp+=w;
	return 1;
}
void Dfs(int x,int pre,int w)
{
	dep[x]=dep[pre]+1;fa[x][0]=pre;mx[w&1][x][0]=w;
	for(int i=1;(1<<i)<dep[x];i++)
	{
		fa[x][i]=fa[fa[x][i-1]][i-1];
		mx[0][x][i]=max(mx[0][x][i-1],mx[0][fa[x][i-1]][i-1]);
		mx[1][x][i]=max(mx[1][x][i-1],mx[1][fa[x][i-1]][i-1]);
	}
	for(auto v:e[x])
	{
		if(v.first==pre) continue;
		Dfs(v.first,x,v.second);
	}
	return;
}
int LCA(int x,int y,int tp)
{
	if(dep[x]>dep[y]) swap(x,y);
	int res=0;
	for(int i=19;i>=0;i--) if(dep[fa[y][i]]>=dep[x]) res=max(res,mx[tp][y][i]),y=fa[y][i];
	for(int i=19;i>=0;i--) if(fa[x][i]!=fa[y][i]) res=max(res,max(mx[tp][x][i],mx[tp][y][i])),x=fa[x][i],y=fa[y][i];
	res=max(res,max(mx[tp][x][0],mx[tp][y][0]));
	return res;
}
void solve()
{
	int cnt=0,s;ans[0]=ans[1]=-1;
	sort(edg+1,edg+m+1);tmp=0;
	for(int i=1;i<=n;i++) f[i]=i;
	for(int i=1;i<=m;i++)
	{
		if(merge(edg[i].u,edg[i].v,edg[i].w)) cnt++,vis[i]=true;
		if(cnt==n-1) break;
	}
	if(cnt!=n-1) return;
	ans[tmp&1]=tmp;ans[(tmp&1)^1]=IINF;
	Dfs(1,0,0);
	for(int i=1;i<=m;i++)
	{
		if(!vis[i])
		{
			s=LCA(edg[i].u,edg[i].v,(edg[i].w&1)^1);
			if(s) ans[(tmp&1)^1]=min(ans[(tmp&1)^1],tmp+edg[i].w-s);
		}
	}
	if(ans[(tmp&1)^1]==IINF) ans[(tmp&1)^1]=-1;
	return;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++) cin>>edg[i].u>>edg[i].v>>edg[i].w;
		solve();
		cout<<ans[0]<<" "<<ans[1]<<"\n";
		for(int i=1;i<=n;i++) e[i].clear();
		for(int i=1;i<=m;i++) vis[i]=false;
		for(int i=1;i<=n;i++) for(int j=0;j<20;j++) mx[0][i][j]=mx[1][i][j]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 105ms
memory: 16956kb

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 1314022773
1263932600 1307926149
1189242112 1180186165
2248358640 2173083215
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1410162043
3456885934 3486092007
3943951826 3628098353
2479987500 2535532661
2909126794 267...

result:

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