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)
- 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<char> dp(n + 1, 0), dp1;
- reverse(all(s));
- dp[0] = 1;
- forn(i, 0, s.sz()){
- dp1.clear(); dp1.assign(n + 1, 0);
- forn(j, 0, n - s[i] + 1) dp1[j + s[i]] = dp[j];
- forn(j, 0, n + 1) dp[j] |= dp1[j];
- }
- return dp[n / 2];
- }
- int main(){
- freopen("network4.txt", "r", stdin);
- // freopen("tests.in", "r", stdin);
- int n, m, f, t, w, l = 0, 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);
- }
- 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