QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#539453#8941. Even or Odd Spanning Treeucup-team052#WA 80ms18028kbC++232.6kb2024-08-31 14:52:422024-08-31 14:52:49

Judging History

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

  • [2024-08-31 14:52:49]
  • 评测
  • 测评结果:WA
  • 用时:80ms
  • 内存:18028kb
  • [2024-08-31 14:52:42]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

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

const int N = 5e5 + 5, INF = 2e9;

vector <pii> adj[N];
int f[N], u[N], v[N], w[N], id[N], used[N], fa[N / 2][20], mx[N / 2][20][2], dep[N], ww[N];
int T, n, m;

bool cmp(int i, int j) {
	return w[i] < w[j];
}

int find(int x) {
	return f[x] == x ? x : f[x] = find(f[x]);
}

void dfs1(int u) {
	mx[u][0][0] = mx[u][0][1] = 0;
	mx[u][0][ww[u] % 2] = ww[u];
	for (int i = 1; i <= 17; i++) {
		fa[u][i] = fa[fa[u][i - 1]][i - 1];
		for (int j = 0; j <= 1; j++) {
			mx[u][i][j] = max(mx[u][i - 1][j], mx[fa[u][i - 1]][i - 1][j]);
		}
	}
	for (auto v : adj[u]) {
		if (v.first == fa[u][0]) continue;
		fa[v.first][0] = u; ww[v.first] = v.second; dep[v.first] = dep[u] + 1; dfs1(v.first);
	}
}

int jump(int u, int k) {
	for (int i = 0; k; i++) {
		if ((k >> i) & 1) {
			u = fa[u][k];
			k ^= (1 << i);
		}
	}
	return u;
}

int query(int u, int k, int o) {
	int ans = 0;
	for (int i = 0; k; i++) {
		if ((k >> i) & 1) {
			ans = max(ans, mx[u][k][o]);
			u = fa[u][k];
			k ^= (1 << i);
		}
	}
	return ans;
}

int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	u = jump(u, dep[u] - dep[v]);
	for (int i = 17; i >= 0; i--) {
		if (fa[u][i] != fa[v][i]) {
			u = fa[u][i];
			v = fa[v][i];
		}
	}
	return fa[u][0];
}

int qry(int u, int v, int o) {
	int d = lca(u, v);
	return max(query(u, dep[u] - dep[d], o), query(v, dep[v] - dep[d], o));
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> T;
	while (T--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++) f[i] = i;
		for (int i = 1; i <= n; i++) adj[i].clear();
		for (int i = 1; i <= m; i++) {
			cin >> u[i] >> v[i] >> w[i];
			id[i] = i;
		}
		sort(id + 1, id + m + 1);
		ll sum = 0;
		int mcnt = 0;
		for (int i = 1; i <= m; i++) {
			int x = find(u[id[i]]), y = find(v[id[i]]);
			if (x == y) used[id[i]] = 0;
			else {
				++mcnt;
				adj[u[id[i]]].emplace_back(v[i], w[id[i]]);
				adj[v[id[i]]].emplace_back(u[i], w[id[i]]);
				f[x] = y;
				used[id[i]] = 1;
				sum += w[id[i]];
			}
		}
		if (mcnt != n - 1) {
			cout << -1 << ' ' << -1 << '\n';
			continue;
		}
		dfs1(1);
		int ans = INF;
		for (int i = 1; i <= m; i++) {
			if (!used[i]) {
				int res = query(u[i], v[i], (w[i] % 2) ^ 1);
				if (res) ans = min(ans, w[i] - res);
			}
		}
		ll ans0 = -1, ans1 = -1;
		if (sum % 2 == 0) {
			ans0 = sum;
			if (ans != INF) ans1 = sum + ans;
		} else {
			ans1 = sum;
			if (ans != INF) ans0 = sum + ans;
		}
		cout << ans0 << ' ' << ans1 << '\n';
		
	}
	return 0;
}

详细

Test #1:

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

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: 80ms
memory: 17912kb

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:

8576369008 7806420739
5584321972 6433454271
6477876480 5781639311
5524260924 6474596479
6630194602 5846350165
8636859846 7721444743
3297802200 2646125113
5640426354 5179910421
4845493372 5736207983
5854765706 6610200235
7662319324 8529091615
9546316230 8724625431
10620505162 9685234837
5462089166 64...

result:

wrong answer 1st numbers differ - expected: '3140067932', found: '8576369008'