Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz size
- #define ll long long
- #define ld long double
- #define fs first
- #define sc second
- #define forn(i, f, t) for(int i = f; i < t; i++)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- const int INF = 2147483647;
- const int MOD = 1000000007;
- const ll INF64 = 9223372036854775807;
- const ld EPS = 1e-7;
- using namespace std;
- int findR(int v, vector<int>& p){
- return (v == p[v] ? v : p[v] = findR(p[v], p));
- }
- void unionS(int v1, int v2, vector<int>& p){
- p[findR(v2, p)] = findR(v1, p);
- }
- bool check(vector<vector<pair<int, int> > > d, int m){
- int n = d.sz();
- vector<vector<int> > g(n);
- forn(i, 0, n)
- forn(j, 0, d[i].sz())
- if (d[i][j].sc > m)
- g[i].pb(d[i][j].fs);
- vector<int> p, s(n, 0);
- forn(i, 0, n)
- p.pb(i);
- forn(i, 0, n)
- forn(j, 0, g[i].sz())
- unionS(i, g[i][j], p);
- // forn(i, 0, n) printf("%d ", p[i]); printf("\n");
- forn(i, 0, n)
- s[p[i]]++;
- sort(all(s));
- reverse(all(s));
- int z;
- for(z = 0; z < n; z++)
- if (s[z] == 0) break;
- s.resize(z);
- vector<vector<int> > dp(s.sz());
- forn(i, 0, s.sz())
- forn(j, 0, n + 1)
- dp[i].pb(0);
- reverse(all(s));
- // forn(i, 0, s.sz()) printf("%d ", s[i]); printf("\n");
- forn(i, 1, s.sz())
- forn(c, 0, n + 1)
- dp[i][c] = (c < s[i] ? dp[i - 1][c] : max(dp[i - 1][c], dp[i - 1][c - s[i]] + s[i]));
- // forn(i, 0, n + 1) printf("%d ", dp[s.sz() - 1][i]); printf("\n\n");
- return dp[s.sz() - 1][n / 2] == n / 2;
- }
- int main(){
- // freopen("network4.txt", "r", stdin);
- freopen("tests.in", "r", stdin);
- int n, m, f, t, w, l = INF, r = 0;
- scanf("%d%d", &n, &m);
- vector<vector<pair<int, int> > > d(n);
- forn(i, 0, m){
- scanf("%d%d%d", &f, &t, &w); --f; --t;
- d[f].pb(mp(t, w));
- d[t].pb(mp(f, w));
- r = max(r, w);
- l = min(l, w);
- }
- while (l < r - 1){
- int m = (l + r) / 2;
- if (check(d, m)) r = m;
- else l = m;
- }
- printf("%d\n", check(d, l) ? l : r);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement