QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#585597#8941. Even or Odd Spanning TreexhguaWA 89ms48488kbC++142.6kb2024-09-23 21:25:002024-09-23 21:25:00

Judging History

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

  • [2024-09-23 21:25:00]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:48488kb
  • [2024-09-23 21:25:00]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 2e5 + 5, M = 5e5 + 5, LOG = 20, INF = (1 << 30);

struct DSU {
	int n, fa[N], siz[N];
	void init(int _n) { n = _n; for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1; }
	int father(int u) { return fa[u] = (fa[u] == u ? u : father(fa[u])); }
	void merge(int u, int v) {
		u = father(u), v = father(v);
		if (u == v) return;
		if (siz[u] < siz[v]) std::swap(u, v);
		fa[v] = u; siz[u] += siz[v];
	} 
	bool isSame(int u, int v) { return father(u) == father(v); }
} dsu;

int T, n, m;
int dep[N], st[2][LOG][N], fa[LOG][N];
bool mark[M];
std::array<int, 3> E[M];
std::vector<std::array<int, 2>> G[N];

void dfs(int u, int f) {
	dep[u] = dep[f] + 1;
	for (int i = 1; i < LOG; i++) {
		fa[i][u] = fa[i - 1][fa[i - 1][u]];
		st[0][i][u] = std::max(st[0][i - 1][u], st[0][i - 1][fa[i - 1][u]]);
		st[1][i][u] = std::max(st[1][i - 1][u], st[1][i - 1][fa[i - 1][u]]);
	}
	for (auto e : G[u]) {
		int v = e[0], w = e[1]; if (v == f) continue;
		st[w & 1][0][v] = w; fa[0][v] = u; dfs(v, u);
	}
}

int jump(int u, int v, int o) {
	if (dep[u] < dep[v]) std::swap(u, v);
	int ret = 0;
	for (int i = LOG - 1; ~i; i--) {
		if (dep[fa[i][u]] > dep[v]) {
			ret = std::max(ret, st[o ^ 1][i][u]);
			u = fa[i][u];
		}
	}
	u = fa[0][u]; if (u == v) return ret;
	for (int i = LOG - 1; ~i; i--) {
		if (fa[i][u] != fa[i][v]) {
			ret = std::max(ret, st[o ^ 1][i][u]);
			ret = std::max(ret, st[o ^ 1][i][v]);
			u = fa[i][u], v = fa[i][v];
		}
	}
	return ret;
}

void solve() {
	std::cin >> n >> m; dsu.init(n);
	for (int i = 1; i <= n; i++) {
		G[i].clear();
		for (int j = 0; j < LOG; j++) st[0][j][i] = st[1][j][i] = 0;
	}
	for (int i = 1; i <= m; i++) {
		int u, v, w; std::cin >> u >> v >> w;
		E[i] = {w, u, v}; mark[i] = 0;
	}
	std::sort(E + 1, E + 1 + m);
	i64 res = 0, ans[2] = {-1, -1}; int cnt = 0; 
	for (int i = 1; i <= m; i++) {
		int u = E[i][1], v = E[i][2], w = E[i][0];
		if (!dsu.isSame(u, v)) {
			cnt++; res += w; dsu.merge(u, v);
			G[u].push_back({v, w}); 
			G[v].push_back({u, w});
			mark[i] = 1;
		}
	}
	if (cnt != n - 1) return std::cout << "-1" << " " << "-1" << "\n", void();
	ans[res & 1] = res; dfs(1, 0);
	for (int i = 1; i <= m; i++) if (!mark[i]) {
		int u = E[i][1], v = E[i][2], w = E[i][0], max = jump(u, v, w & 1);
		if (max != 0) {
			ans[res & 1 ^ 1] = res - max + w;
			break;
		}
	}
	std::cout << ans[0] << " " << ans[1] << "\n";
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	std::cin >> T;
	while (T--) solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 89ms
memory: 48488kb

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 1565384431
1426868318 1180186165
2248358640 2491709271
3911635494 3738936375
1116515874 1058677117
3433711014 3402127725
1201099898 1187873655
1395482806 1587597625
3456885934 3549237773
3943951826 3694995349
2479987500 2535532661
2909126794 309...

result:

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