QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#559454 | #7415. Fast Spanning Tree | Z_drj | WA | 0ms | 15148kb | C++14 | 1.8kb | 2024-09-11 22:08:05 | 2024-09-11 22:08:07 |
Judging History
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;
}
Details
Tip: Click on the bar to expand more detailed information
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'