QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#559454#7415. Fast Spanning TreeZ_drjWA 0ms15148kbC++141.8kb2024-09-11 22:08:052024-09-11 22:08:07

Judging History

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

  • [2024-09-11 22:08:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:15148kb
  • [2024-09-11 22:08:05]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

#define PII std::pair<int, int>

const int N = 3E5 + 5;
const int INF = 1E6;

int n, m;
int w[N];

struct Edge {
	int u, v, s;
}e[N];

int fa[N];

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

std::priority_queue<int, std::vector<int>, std::greater<int>> edge;
std::priority_queue<PII, std::vector<PII>, std::greater<PII>> q[N];

void update(int x) {
	int u = e[x].u, v = e[x].v, s = e[x].s;
	u = find(u), v = find(v);
	
	if (u == v) {
		return;
	}

	if (w[u] + w[v] >= s) {
		edge.push(x);
	} else {
		q[u].push(std::make_pair((s - w[u] + w[v] + 1) / 2, x));
		q[v].push(std::make_pair((s - w[v] + w[u] + 1) / 2, x));
	}
}

std::vector<int> ans;

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

	std::cin >> n >> m;

	for (int i = 1; i <= n; i++) {
		std::cin >> w[i];
		fa[i] = i;
	}

	for (int i = 1; i <= m; i++) {
		std::cin >> e[i].u >> e[i].v >> e[i].s;
		update(i);
	}

	while (!edge.empty()) {
		int x = edge.top();
		edge.pop();

		int u = e[x].u, v = e[x].v;
		u = find(u), v = find(v);

		if (u == v) {
			continue;
		}

		ans.push_back(x);
		

		if (q[u].size() < q[v].size()) {
			std::swap(u, v);
		}

		fa[v] = u, w[u] += w[v];

		w[u] = std::min(w[u], INF);

		while (!q[v].empty()) {
			auto tmp = q[v].top();
			q[v].pop();

			if (w[u] >= tmp.first) {
				update(tmp.second);
			} else {
				q[u].push(tmp);
			}
		}

		while (!q[u].empty()) {
			auto tmp = q[u].top();
			q[u].pop();

			if (w[u] >= tmp.first) {
				update(tmp.second);
			} else {
				q[u].push(tmp);
				break;
			}
		}
	}

	std::cout << ans.size() << "\n";
	for (auto i : ans) {
		std::cout << i << " ";
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 15148kb

input:

5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

output:

4
2 3 4 1 

result:

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