#include #include #include using namespace std; using namespace atcoder; using ll = long long; #define all(A) A.begin(),A.end() using vll = vector; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) using Graph = vector>; Graph G; vll E; vector seen; ll k = 0; void dfs(int v) { seen[v] = true; // v を訪問済にする // v から行ける各頂点 next_v について E[v] = k; for (auto next_v : G[v]) { if (seen[next_v]) continue; // next_v が探索済だったらスルー k++; dfs(next_v); // 再帰的に探索 } E[v] = k - E[v]; } int main() { ll N; cin >> N; dsu D(N); while (1) { ll A, B, C; cin >> A >> B >> C; A--; B--; D.merge(A, B); if (D.size(A) == N) { cout << C << endl; return 0; } } }