Task: | Tietoverkko |
Sender: | hltk |
Submission time: | 2021-10-04 00:49:54 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 10 |
#2 | ACCEPTED | 15 |
#3 | ACCEPTED | 75 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
#2 | ACCEPTED | 0.01 s | 2, 3 | details |
#3 | ACCEPTED | 0.24 s | 3 | details |
Code
#include <algorithm> #include <iostream> #include <tuple> #include <vector> using namespace std; struct UF { vector<int> p; UF(int n) : p(n, -1) {} int root(int s) { return p[s] < 0 ? s : p[s] = root(p[s]); } int sz(int s) { return -p[root(s)]; } void join(int a, int b) { a = root(a); b = root(b); p[b] += p[a]; p[a] = b; } }; int main() { int n; cin >> n; vector<tuple<int, int, int>> e; for (int i = 0; i < n - 1; ++i) { int a, b, x; cin >> a >> b >> x; e.emplace_back(x, a - 1, b - 1); } sort(e.rbegin(), e.rend()); long r = 0; UF uf(n); for (auto [x, a, b] : e) { r += 1ll * x * uf.sz(a) * uf.sz(b); uf.join(a, b); } cout << r << endl; }
Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
input |
---|
100 1 2 74 1 3 100 2 4 50 3 5 40 ... |
correct output |
---|
88687 |
user output |
---|
88687 |
Test 2
Group: 2, 3
Verdict: ACCEPTED
input |
---|
5000 1 2 613084013 1 3 832364259 2 4 411999902 3 5 989696303 ... |
correct output |
---|
1103702320243776 |
user output |
---|
1103702320243776 |
Test 3
Group: 3
Verdict: ACCEPTED
input |
---|
200000 1 2 613084013 1 3 832364259 2 4 411999902 3 5 989696303 ... |
correct output |
---|
1080549209850010931 |
user output |
---|
1080549209850010931 |