Advertisement
Guest User

Untitled

a guest
May 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. /*
  2.     Advanced Algorithms - Homework 2
  3.     Team: Yunus Inan, Nihal Ezgi Yuceturk, Mammad Hajili
  4. */
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. vector<int> parent, rnk;
  8. int find_parent(int node)
  9. {
  10.     if (parent[node] == -1)
  11.         return node;
  12.     return find_parent(parent[node]);
  13. }
  14. void union_find(int x, int y)
  15. {
  16.     int px = find_parent(x);
  17.     int py = find_parent(y);
  18.     if(rnk[px] < rnk[py]) parent[px] = py;
  19.     else if(rnk[px] > rnk[py]) parent[py] = px;
  20.     else{
  21.        parent[py] = px;
  22.        rnk[px] ++;
  23.     }
  24. }
  25.  
  26. vector<pair<int, int>> edges;
  27. set< set<int> > S;
  28. int n, m;
  29. pair<int, set<int> > min_cut() {
  30.  
  31.     int n_vertices = n;
  32.     set<int> contracts;
  33.     while (n_vertices > 2)  {
  34.         int ind = rand();
  35.         ind = ind % m;
  36.         int p1 = find_parent(edges[ind].first);
  37.         int p2 = find_parent(edges[ind].second);
  38.         if (p1 == p2) continue;
  39.         else {
  40.             n_vertices--;
  41.             union_find(p1, p2);
  42.         }
  43.     }
  44.     int cut = 0;
  45.     for (int j = 0; j < m; ++ j) {
  46.         int p1 = find_parent(edges[j].first);
  47.         int p2 = find_parent(edges[j].second);
  48.         if (p1 != p2) {
  49.             contracts.insert(j);
  50.             cut ++;
  51.         }
  52.     }
  53.  
  54.     return {cut, contracts};
  55. }
  56. int main() {
  57.     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  58.     cin >> n >> m;
  59.     for(int i = 0; i < m; ++ i) {
  60.         int u, v;
  61.         cin >> u >> v;
  62.         u --;
  63.         v --;
  64.         edges.push_back({u, v});
  65.     }
  66.     int mn_cut = INT_MAX;
  67.  
  68.     for(int i = 0; i < 150000; ++ i) {
  69.         parent = vector<int>(n, -1);
  70.         rnk = vector<int>(n, 0);
  71.         auto cut = min_cut();
  72.         mn_cut = min(mn_cut, cut.first);
  73.     }
  74.  
  75.     int sz = 0;
  76.     for(int i = 0; i < 500000; ++ i)  {
  77.         parent = vector<int>(n, -1);
  78.         rnk = vector<int>(n, 0);
  79.         auto cut = min_cut();
  80.  
  81.         if(cut.first == mn_cut) {
  82.             S.insert(cut.second);
  83.         }
  84.  
  85.         if(i % 10000 == 0 and i != 0) {
  86.             if(sz == S.size()) break;
  87.             sz = S.size();
  88.         }
  89.     }
  90.     cout << mn_cut << " " << S.size() << endl;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement