From cf3a9090478c71ffd1714e3deff46f8b4a715da3 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Wed, 27 Apr 2016 12:16:39 +0800 Subject: [PATCH] From http://blog.chinaunix.net/uid-25304689-id-349853.html --- TYVJ/1403_chinaunix.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 TYVJ/1403_chinaunix.cpp diff --git a/TYVJ/1403_chinaunix.cpp b/TYVJ/1403_chinaunix.cpp new file mode 100644 index 0000000..8f18da1 --- /dev/null +++ b/TYVJ/1403_chinaunix.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int M = 100000; +const int N = 20000; + +struct EDGE { + int a; + int b; + int v; +} edge[M]; + +int f[N * 2]; + +bool Compare(const EDGE &a, const EDGE &b) +{ + return a.v < b.v; +} + +int Find(int x) +{ + if (f[x] == x) { + return f[x]; + } else { + return f[x] = Find(f[x]); + } +} + +int main() +{ + int n, m; + + ios::sync_with_stdio(false); + + cin >> n; + cin >> m; + for (int i = 0; i < m; i++) { + cin >> edge[i].a; + cin >> edge[i].b; + cin >> edge[i].v; + } + + for (int i = 0; i < n * 2; i++) { + f[i] = i; + } + + sort(edge, edge + m, Compare); + + for (int i = m - 1; i >= 0; i--) { + int x = Find(edge[i].a); + int y = Find(edge[i].b); + + if (x == y) { + cout << edge[i].v << endl; + + return 0; + } + + int xx = Find(edge[i].a + n); + int yy = Find(edge[i].b + n); + + f[x] = yy; + f[y] = xx; + } + + cout << "0" << endl; + + return 0; +}