Advertisement
PikMike

Untitled

Apr 10th, 2016
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pb push_back
  4. #define mp make_pair
  5. #define sz size
  6. #define ll long long
  7. #define ld long double
  8. #define fs first
  9. #define sc second
  10. #define forn(i, f, t) for(int i = f; i < t; i++)
  11. #define all(x) (x).begin(), (x).end()
  12. #define ins insert
  13.  
  14. const int INF = 2147483647;
  15. const int MOD = 1000000007;
  16. const ll INF64 = 9223372036854775807;
  17. const ld EPS = 1e-7;
  18.  
  19. using namespace std;
  20.  
  21.  
  22. int findR(int v, vector<int>& p){
  23.     return (v == p[v] ? v : p[v] = findR(p[v], p));
  24. }
  25.  
  26.  
  27. void unionS(int v1, int v2, vector<int>& p){
  28.     p[findR(v2, p)] = findR(v1, p);
  29. }
  30.  
  31.  
  32. bool check(vector<vector<pair<int, int> > >& d, int m){
  33.     int n = d.sz();
  34.     vector<vector<int> > g(n);
  35.     forn(i, 0, n)
  36.         forn(j, 0, d[i].sz())
  37.             if (d[i][j].sc > m)
  38.                 g[i].pb(d[i][j].fs);
  39.     vector<int> p, s(n, 0);
  40.     forn(i, 0, n)
  41.         p.pb(i);
  42.     forn(i, 0, n)
  43.         forn(j, 0, g[i].sz())
  44.             unionS(i, g[i][j], p);
  45.     forn(i, 0, n)
  46.         s[p[i]]++;
  47.     sort(all(s));
  48.     reverse(all(s));
  49.     int z;
  50.     for(z = 0; z < n; z++)
  51.         if (s[z] == 0) break;
  52.     s.resize(z);
  53.     vector<char> dp(n + 1, 0), dp1;
  54.     reverse(all(s));
  55.     dp[0] = 1;
  56.     forn(i, 0, s.sz()){
  57.         dp1.clear(); dp1.assign(n + 1, 0);
  58.         forn(j, 0, n - s[i] + 1) dp1[j + s[i]] = dp[j];
  59.         forn(j, 0, n + 1) dp[j] |= dp1[j];
  60.     }
  61.     return dp[n / 2];
  62. }
  63.  
  64.  
  65. int main(){
  66.     freopen("network4.txt", "r", stdin);
  67.     // freopen("tests.in", "r", stdin);
  68.     int n, m, f, t, w, l = 0, r = 0;
  69.     scanf("%d%d", &n, &m);
  70.     vector<vector<pair<int, int> > > d(n);
  71.     forn(i, 0, m){
  72.         scanf("%d%d%d", &f, &t, &w); --f; --t;
  73.         d[f].pb(mp(t, w));
  74.         d[t].pb(mp(f, w));
  75.         r = max(r, w);
  76.     }
  77.     while (l < r - 1){
  78.         int m = (l + r) / 2;
  79.         if (check(d, m)) r = m;
  80.         else l = m;
  81.     }
  82.     printf("%d\n", check(d, l) ? l : r);
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement