Wednesday, February 13, 2019

[UVA] 10600 - ACM Contest and Blackout

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 10600 - ACM Contest and Blackout
Source            : UVA Online Judge
Category          : Graph Theory
Algorithm         : Minimum Spanning Tree, kth mst
Verdict           : Accepted
Complexity : O(km), k = kth spanning tree, m = edges

  1. #include "bits/stdc++.h"  
  2. #include "ext/pb_ds/assoc_container.hpp"  
  3. #include "ext/pb_ds/tree_policy.hpp"  
  4. #include "ext/rope"  
  5.   
  6. using namespace std;  
  7. using namespace __gnu_pbds;  
  8. using namespace __gnu_cxx;  
  9.   
  10. #define FI              freopen("in.txt", "r", stdin)  
  11. #define FO              freopen("out.txt", "w", stdout)  
  12. #define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)  
  13.   
  14. #define FOR(i, n)       for (int i = 1; i <= n; i++)  
  15. #define For(i, n)       for (int i = 0; i < n; i++)  
  16. #define ROF(i, n)       for (int i = n; i >= 1; i--)  
  17. #define Rof(i, n)       for (int i = n-1; i >= 0; i--)  
  18. #define FORI(i, n)      for (auto i : n)  
  19. #define FORAB(i, a, b)  for (int i = a; i <= b; i++)  
  20.   
  21. #define ll              long long  
  22. #define ull             unsigned long long  
  23. #define vi              vector <int>  
  24. #define vl              vector <ll>  
  25. #define pii             pair <int, int>  
  26. #define pll             pair <ll, ll>  
  27. #define mk              make_pair  
  28. #define ff              first  
  29. #define ss              second  
  30. #define eb              emplace_back  
  31. #define em              emplace  
  32. #define pb              push_back  
  33. #define ppb             pop_back  
  34. #define All(a)          a.begin(), a.end()  
  35. #define memo(a, b)      memset(a, b, sizeof a)  
  36. #define Sort(a)         sort(All(a))  
  37. #define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())  
  38. #define rev(a)          reverse(All(a))  
  39. #define sz(a)           (int)a.size()  
  40. #define max3(a, b, c)   max(a, max(b, c))  
  41. #define min3(a, b, c)   min(a, min(b, c))  
  42. #define maxAll(a)       *max_element(All(a))  
  43. #define minAll(a)       *min_element(All(a))  
  44. #define allUpper(a)     transform(All(a), a.begin(), :: toupper)  
  45. #define allLower(a)     transform(All(a), a.begin(), :: tolower)  
  46. #define endl            '\n'  
  47. #define nl              puts("")  
  48. #define ub              upper_bound  
  49. #define lb              lower_bound  
  50. #define Exp             exp(1.0)  
  51. #define PIE             2*acos(0.0)  
  52. #define Sin(a)          sin(((a)*PIE)/180.0)  
  53. #define EPS             1e-9  
  54.   
  55. template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;  
  56.   
  57. // rope <int> Rope;  
  58.   
  59. // int dr[] = {1, -1, 0, 0}; // 4 Direction  
  60. // int dc[] = {0, 0, 1, -1};  
  61. // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction  
  62. // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};  
  63. // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves  
  64. // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};  
  65.   
  66. #define here                                cerr << "Here" << endl;  
  67. #define trace1(x)                           cerr << #x << ": " << x << endl;  
  68. #define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;  
  69. #define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;  
  70. #define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;  
  71. #define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;  
  72. #define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;  
  73.   
  74. inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }  
  75. inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }  
  76. inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }  
  77. inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }  
  78.   
  79. #define ones(mask)                          __builtin_popcount(mask)   // count set bit  
  80. #define onesLL(mask)                        __builtin_popcountll(mask) // for long long  
  81. #define lzeros(mask)                        __builtin_clz(mask)        // no of leading zeros  
  82. #define tzeros(mask)                        __builtin_ctz(mask)        // no of trailing zeros  
  83.   
  84. inline int read()                           { int a; scanf("%d", &a); return a; }  
  85. inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }  
  86. inline double readDD()                      { double a; scanf("%lf", &a); return a; }  
  87.   
  88. template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }  
  89. int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }  
  90. ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }  
  91.   
  92. #define inf             1e8  
  93. #define mod             1000000007  
  94.   
  95. static const int maxn = 200 + 5;  
  96. static const int logn = 18;  
  97.   
  98. struct Edge  
  99. {  
  100.       int u, v, w;  
  101.       Edge(int u = 0, int v = 0, int w = 0) : u(u), v(v), w(w) {}  
  102.   
  103.       friend bool operator < (Edge p, Edge q)  
  104.       {  
  105.             return p.w < q.w;  
  106.       }  
  107. };  
  108.   
  109. struct Partition  
  110. {  
  111.       vi choices;  
  112.       int mstCost;  
  113.       vi mstEdges;  
  114.       Partition(vi choices, int mstCost, vi mstEdges)  
  115.       {  
  116.             this->choices = choices;  
  117.             this->mstCost = mstCost;  
  118.             this->mstEdges = mstEdges;  
  119.       }  
  120.       friend bool operator < (Partition p, Partition q)  
  121.       {  
  122.             return p.mstCost > q.mstCost;  
  123.       }  
  124. };  
  125.   
  126. int tnode, tedge;  
  127. vector <Edge> graph;  
  128.   
  129. int par[maxn];  
  130.   
  131. void makeSet()  
  132. {  
  133.       FOR(i, tnode) par[i] = i;  
  134. }  
  135.   
  136. int findRep(int r)  
  137. {  
  138.       if (r == par[r]) return r;  
  139.       return par[r] = findRep(par[r]);  
  140. }  
  141.   
  142. bool Union(int u, int v)  
  143. {  
  144.       int p = findRep(u);  
  145.       int q = findRep(v);  
  146.       if (p == q) return false;  
  147.       par[q] = p;  
  148.       return true;  
  149. }  
  150.   
  151. Partition createPartition(vi choices)  
  152. {  
  153.       makeSet();  
  154.       int take = 0;  
  155.       int ptr = 0;  
  156.       int mstCost = 0;  
  157.       vi mstEdges(tedge+1, -1);  
  158.       for (int i = 0; i < tedge; i++)  
  159.       {  
  160.             if (choices[i] == 1)  
  161.             {  
  162.                   Edge e = graph[i];  
  163.                   Union(e.u, e.v);  
  164.                   mstCost += e.w;  
  165.                   mstEdges[ptr++] = i;  
  166.                   ++take;  
  167.             }  
  168.       }  
  169.       if (take == tnode-1) return Partition(choices, mstCost, mstEdges);  
  170.       for (int i = 0; i < tedge; i++)  
  171.       {  
  172.             if (take == tnode-1) break;  
  173.             if (choices[i] == 0)  
  174.             {  
  175.                   Edge e = graph[i];  
  176.                   if (Union(e.u, e.v))  
  177.                   {  
  178.                         mstCost += e.w;  
  179.                         mstEdges[ptr++] = i;  
  180.                         ++take;  
  181.                   }  
  182.             }  
  183.       }  
  184.       sort(mstEdges.begin(), mstEdges.begin()+ptr);  
  185.       if (take == tnode-1) return Partition(choices, mstCost, mstEdges);  
  186.       return Partition(choices, -1, mstEdges);  
  187. }  
  188.   
  189. vi solve(int k)  
  190. {  
  191.       vi Mst;  
  192.       priority_queue <Partition> PQ;  
  193.       vi choices(tedge+1);  
  194.       Partition part = createPartition(choices);  
  195.       assert(part.mstCost != -1);  
  196.       PQ.emplace(part);  
  197.   
  198.       while (PQ.empty() == false)  
  199.       {  
  200.             Partition p = PQ.top(); PQ.pop();  
  201.             Mst.eb(p.mstCost);  
  202.             --k;  
  203.   
  204.             if (k == 0) return Mst;  
  205.   
  206.             for (int i = 0; i < tnode-1; i++)  
  207.             {  
  208.                   if (p.choices[ p.mstEdges[i] ] == 0)  
  209.                   {  
  210.                         choices = p.choices;  
  211.                         choices[ p.mstEdges[i] ] = -1;  
  212.                         for (int j = 0; j < i; j++)  
  213.                         {  
  214.                               choices[ p.mstEdges[j] ] = 1;  
  215.                         }  
  216.                         Partition nxt = createPartition(choices);  
  217.                         if (nxt.mstCost != -1) PQ.emplace(nxt);  
  218.                   }  
  219.             }  
  220.       }  
  221.       return Mst;  
  222. }  
  223.   
  224. int main()  
  225. {  
  226.       FI;  
  227.   
  228.       int tc = read();  
  229.       FOR(tcase, tc)  
  230.       {  
  231.             tnode = read();  
  232.             tedge = read();  
  233.             For(i, tedge)  
  234.             {  
  235.                   int u = read();  
  236.                   int v = read();  
  237.                   int w = read();  
  238.                   graph.eb(u, v, w);  
  239.             }  
  240.             Sort(graph);  
  241.             vi mst = solve(2);  
  242.             assert(sz(mst) == 2);  
  243.             printf("%d %d\n", mst[0], mst[1]);  
  244.             graph.clear();  
  245.       }  
  246. }  

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.