1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef long long ll;
const int N = 20010; const int M = 200010;
int n, m; int w[M]; int to[M], nxt[M], fir[N], edge_num, len[M]; void addEdge(int u, int v, int w) { to[++edge_num] = v; len[edge_num] = w; nxt[edge_num] = fir[u]; fir[u] = edge_num; }
int W; int col[N];
bool dfs(int u) { for(int i = fir[u]; i; i = nxt[i]) { if(len[i] <= W) continue; int v = to[i]; if(col[u] == col[v]) { return false; } if(!col[v]) { col[v] = 3 - col[u]; if(!dfs(v)) { return false; } } } return true; }
bool check() { for(int i = 1; i <= n; i++) { if(!col[i]) { col[i] = 1; if(!dfs(i)) return false; } } return true; }
int main() { cin >> n >> m; for(int i = 1; i <= m; i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); addEdge(a, b, c); addEdge(b, a, c); w[i] = c; } sort(w + 1, w + m + 1); int l = 1, r = m; int ans = 0; while(l < r) { int mid = l + r >> 1; W = w[mid]; memset(col, 0, sizeof(col)); if(check()) ans = w[mid], r = mid; else l = mid + 1; } if(l == 1 && r < l) cout << 0 << endl; else printf("%d\n", ans);
return 0; }
|