1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
| // 42jerrykim.github.io에서 더 많은 정보를 확인 할 수 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct CandidateEdge {
int u; // component root at scheduling time
int v; // component root at scheduling time
int idx; // edge index
};
struct CandidateCompare {
bool operator()(const CandidateEdge& a, const CandidateEdge& b) const {
return a.idx > b.idx; // min-heap by index
}
};
struct Watcher {
int other; // other component root
long long s; // required threshold
long long half; // current component sum threshold to re-check
int idx; // edge index
};
struct WatcherHalfGreater {
bool operator()(const Watcher& a, const Watcher& b) const {
return a.half > b.half; // min-heap by half
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<long long> compWeight(n + 1);
for (int i = 1; i <= n; ++i) cin >> compWeight[i];
vector<int> parent(n + 1);
iota(parent.begin(), parent.end(), 0);
vector< priority_queue<Watcher, vector<Watcher>, WatcherHalfGreater> > watch(n + 1);
priority_queue<CandidateEdge, vector<CandidateEdge>, CandidateCompare> fin; // eligible edges by smallest index
function<int(int)> findRoot = [&](int x) -> int {
if (parent[x] == x) return x;
return parent[x] = findRoot(parent[x]);
};
auto unite = [&](int a, int b) -> int {
a = findRoot(a);
b = findRoot(b);
if (a == b) return a;
if (watch[a].size() < watch[b].size()) swap(a, b); // small-to-large by watcher heap size
parent[b] = a;
compWeight[a] += compWeight[b];
while (!watch[b].empty()) { watch[a].push(watch[b].top()); watch[b].pop(); }
return a;
};
function<void(int)> processComponent = [&](int root) {
root = findRoot(root);
while (!watch[root].empty()) {
if (watch[root].top().half > compWeight[root]) break;
Watcher w = watch[root].top();
watch[root].pop();
int other = findRoot(w.other);
root = findRoot(root);
if (root == other) continue;
long long sumR = compWeight[root];
long long sumO = compWeight[other];
if (sumR + sumO >= w.s) {
fin.push({root, other, w.idx});
} else {
long long need = (w.s - sumR - sumO + 1) / 2; // positive
watch[root].push(Watcher{other, w.s, sumR + need, w.idx});
watch[other].push(Watcher{root, w.s, sumO + need, w.idx});
}
}
};
for (int i = 1; i <= m; ++i) {
int a, b; long long s; cin >> a >> b >> s;
a = findRoot(a); b = findRoot(b);
if (a == b) continue;
long long sumA = compWeight[a], sumB = compWeight[b];
if (sumA + sumB >= s) {
fin.push({a, b, i});
} else {
long long need = (s - sumA - sumB + 1) / 2;
watch[a].push(Watcher{b, s, sumA + need, i});
watch[b].push(Watcher{a, s, sumB + need, i});
}
}
vector<int> answer; answer.reserve(m);
while (!fin.empty()) {
CandidateEdge cur = fin.top(); fin.pop();
int u = findRoot(cur.u), v = findRoot(cur.v);
if (u == v) continue;
answer.push_back(cur.idx);
int root = unite(u, v);
processComponent(root);
}
cout << (int)answer.size() << '\n';
for (int i = 0; i < (int)answer.size(); ++i) {
if (i) cout << ' ';
cout << answer[i];
}
cout << '\n';
return 0;
}
|