QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#540922#8941. Even or Odd Spanning Treeucup-team3659#WA 117ms18600kbC++142.1kb2024-08-31 18:08:492024-08-31 18:08:50

Judging History

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

  • [2024-08-31 18:08:50]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:18600kb
  • [2024-08-31 18:08:49]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
using pii = pair<int,int>;
const int N = 2e5 + 10, M = 5e5 + 10, D = 18, inf = 1e18;
struct node{int u,v,w;}ed[M];
int f[N], dep[N], fa[N][D+5], is[M];
pii ju[N][D+5];
int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}
vector<pii>g[N];

void dfs(int x, int f)
{
	int i;
	fa[x][0] = f, dep[x] = dep[f] + 1;
	for(i=1;i<=D;i++)
		fa[x][i] = fa[fa[x][i-1]][i-1], ju[x][i] = max(ju[x][i-1],ju[fa[x][i-1]][i-1]);
	for(auto i:g[x])
	{
		int j = i.first, k = i.second;
		if(j==f) continue;
		if(k&1) ju[j][0] = make_pair(inf,k);
		else ju[j][0] = make_pair(k,inf);
		dfs(j,x);
	}
}

pii jump(int a, int b)
{
	pii ans = make_pair(-inf,-inf);
	int i;
	for(i=D;i>=0;i--) if(dep[fa[a][i]]>=dep[b]) ans = max(ans,ju[a][i]), a = fa[a][i];
	for(i=D;i>=0;i--) if(dep[fa[b][i]]>=dep[a]) ans = max(ans,ju[b][i]), b = fa[b][i];
	for(i=D;i>=0;i--) if(fa[a][i]!=fa[b][i])
		ans = max(ans,max(ju[a][i],ju[b][i])), a = fa[a][i], b = fa[b][i];
	if(a!=b) ans = max(ans,max(ju[a][0],ju[b][0]));
	return ans;
}

int Main()
{
	int n, m, i, cnt = 0, ans = 0, ans2 = inf;
	scanf("%lld%lld", &n, &m);
	for(i=1;i<=m;i++) scanf("%lld%lld%lld", &ed[i].u, &ed[i].v, &ed[i].w), is[i] = 0;
	sort(&ed[1],&ed[m]+1,[](node a,node b){return a.w<b.w;});
	for(i=1;i<=n;i++) f[i] = i;
	for(i=1;i<=m;i++)
	{
		int a = find(ed[i].u), b = find(ed[i].v);
		if(a!=b)
		{
			g[ed[i].u].emplace_back(ed[i].v,ed[i].w), g[ed[i].v].emplace_back(ed[i].u,ed[i].w);
			ans += ed[i].w, cnt++;
			is[i] = 1, f[a] = b;
		}
	}
	if(cnt<n-1)
	{
		for(i=1;i<=n;i++) g[i].clear();
		printf("-1 -1\n");
		return 0;
	}
	dfs(1,0);
	for(i=1;i<=m;i++) if(!is[i])
	{
		pair<int,int> x = jump(ed[i].u,ed[i].v);
		if(ed[i].w&1) ans2 = min(ans2,ans-x.first+ed[i].w);
		else ans2 = min(ans2,ans-x.second+ed[i].w);
	}
	if(ans2>=inf/10) ans2 = -1;
	if(ans&1) printf("%lld %lld\n", ans2, ans);
	else printf("%lld %lld\n", ans, ans2);
	for(i=1;i<=n;i++) g[i].clear();
	return 0;
}
signed main(){int t;scanf("%lld",&t);while(t--)Main();return 0;}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 15136kb

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: 117ms
memory: 18600kb

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 -999999996777485917
1262790434 -999999998656312471
1263932600 -999999998351837610
-999999998492471286 1180186165
2248358640 -999999997481660445
-999999995959168830 3738936375
-999999998796105506 1058677117
-999999996406638896 3402127725
-999999998723076091 1187873655
1395482806 -999999998...

result:

wrong answer 2nd numbers differ - expected: '3159441841', found: '-999999996777485917'