/* -*- coding: utf-8 -*- * * 1639.cc: No.1639 最小通信路 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100; const int MAX_L = 101; /* typedef */ struct UFT { int links[MAX_N], ranks[MAX_N], sizes[MAX_N]; UFT() {} void init(int n) { for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1; } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ char s[MAX_L + 4]; UFT uft; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); uft.init(n); int m = n * (n - 1) / 2; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d%s", &a, &b, s); a--, b--; int r = uft.merge(a, b); if (uft.size(r) == n) { puts(s); break; } } return 0; }