CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:hltk
Submission time:2021-10-04 00:49:27 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:44:12: error: 'std::tuple<int, int, int> <anonymous>' has incomplete type
  for (auto [x, a, b] : e) {
            ^~~~~~~~~
In file included from /usr/include/c++/7/vector:69:0,
                 from input/code.cpp:3:
/usr/include/c++/7/bits/vector.tcc: In instantiation of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int, int}; _Tp = std::tuple<int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int> >; std::vector<_Tp, _Alloc>::reference = std::tuple<int, int, int>&]':
input/code.cpp:36:33:   required from here
/usr/include/c++/7/bits/vector.tcc:102:6: error: cannot increment a pointer to incomplete type 'std::tuple<int, int, int>'
      ++this->_M_impl._M_finish;
      ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/7/algorithm:61,
                 from input/code.cpp:1:...

Code

#include <algorithm>
#include <iostream>
#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;
}