QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#553659#8941. Even or Odd Spanning Treeucup-team191#WA 128ms46860kbC++232.5kb2024-09-08 17:20:502024-09-08 17:20:50

Judging History

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

  • [2024-09-08 17:20:50]
  • 评测
  • 测评结果:WA
  • 用时:128ms
  • 内存:46860kb
  • [2024-09-08 17:20:50]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <algorithm>

#define PB push_back
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const int LOG = 18;
const int N = 2e5 + 500;

int n, m, par[N];
vector < pair < int, pii > > edg;
vector < pii > v[N];
int par2[LOG][N], dep[N];
int skok[2][LOG][N];

int pr(int x) {
	if(par[x] == x) return x;
	return par[x] = pr(par[x]);
}

void dfs(int x, int lst) {
	for(auto &[w, y] : v[x]) {
		if(y == lst) continue;
		dep[y] = dep[x] + 1;
		par2[0][y] = x;
		skok[w % 2][0][y] = w;
		skok[1 - (w % 2)][0][y] = -1;
		dfs(y, x);
	}
}

void mrg(int x, int y) {
	par[pr(x)] = pr(y);
}

int get_max(int x, int y, int sto){
	if(dep[x] < dep[y]) swap(x, y);
	int ans = -1;
	for(int j = LOG - 1;j >= 0;j--)
		if((1 << j) >= dep[x] - dep[y]) {
			ans = max(ans, skok[sto][j][x]);
			x = par2[j][x];
		}
	if(x == y) return ans;
	for(int j = LOG - 1;j >= 0;j--) {
		if(par2[j][x] != par2[j][y]) {
			ans = max(ans, max(skok[sto][j][x], skok[sto][j][y]));
			x = par2[j][x];
			y = par2[j][y];
		}
	}
	return max(ans, max(skok[sto][0][x], skok[sto][0][y]));
}

void solve() {
	scanf("%d%d", &n, &m);
	edg.clear(); 
	for(int i = 0;i <= n + 5;i++) {
		for(int j = 0;j < LOG;j++) {
			par2[j][i] = 0;
			skok[0][j][i] = 0;
			skok[1][j][i] = 0;
		}
		v[i].clear();
		par[i] = i;
	}
	for(int i = 0;i < m;i++) {
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		edg.PB({c, {a, b}});
	}
	sort(edg.begin(), edg.end());
	ll ans = 0;
	vector < pair < int, pii > > visak;
	int uzeo = 0;
	for(auto tmp : edg) {
		if(pr(tmp.Y.X) == pr(tmp.Y.Y)) {
			visak.PB(tmp);
		} else {
			uzeo++;
			v[tmp.Y.X].PB({tmp.X, tmp.Y.Y});
			v[tmp.Y.Y].PB({tmp.X, tmp.Y.X});
			ans += tmp.X;
			mrg(tmp.Y.X, tmp.Y.Y);
		}
	}
	if(uzeo != n - 1) {
		printf("-1 -1\n");
		return;
	}
	int ob_ans = -1;
	dfs(1, 1);
	for(int j = 1;j < LOG;j++) {
		for(int i = 1;i <= n;i++) {
			par2[j][i] = par2[j - 1][par2[j - 1][i]];
			skok[0][j][i] = max(skok[0][j - 1][i], skok[0][j - 1][par2[j - 1][i]]);
			skok[1][j][i] = max(skok[1][j - 1][i], skok[1][j - 1][par2[j - 1][i]]);			
		}
	}
	for(auto tmp : visak) {
		int naj = get_max(tmp.Y.X, tmp.Y.Y, 1 - tmp.X % 2);
		if(naj != -1) {
			ob_ans = max(ob_ans, tmp.X - naj);
		}
	}
	ll ans2 = (ob_ans < 0 ? -1 : ans + ob_ans);
	if(ans % 2 == 0) printf("%lld %lld\n", ans, ans2);
	else printf("%lld %lld\n", ans2, ans);	
}

int main() {
	int T; scanf("%d", &T);
	for(;T--;) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 128ms
memory: 46860kb

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 4082296793
1262790434 2124445815
1263932600 1987005195
2130521720 1180186165
2248358640 3214682981
4518352506 3738936375
2041651868 1058677117
4200815683 3402127725
2120010482 1187873655
1395482806 2324672773
3456885934 4351072608
3943951826 4778954193
2479987500 3181995011
2909126794 377...

result:

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