Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <cstring>
  2. #include <vector>
  3. #include <list>
  4. #include <map>
  5. #include <set>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <memory.h>
  21. #include <cassert>
  22.  
  23. using namespace std;
  24.  
  25. struct node {
  26. string id;
  27. string s1,s2;
  28. int cost;
  29. };
  30.  
  31. class comp {
  32.  
  33. public:
  34.  
  35. bool operator () (node a,node b){
  36.  
  37. if (a.cost != b.cost)
  38. return a.cost>b.cost;
  39.  
  40. return a.id > b.id;
  41. }
  42.  
  43. };
  44.  
  45.  
  46. map<string ,int > mp;
  47.  
  48. priority_queue < node, vector<node>, comp > pq;
  49.  
  50. // I will use the set for storing the various values of the
  51. // conditions
  52. int ct=0;
  53.  
  54. int hash(string str){
  55.  
  56. int idx=0;
  57. idx=mp[str];
  58.  
  59. if( idx==0 ){
  60. idx=ct;
  61. mp[str]=ct++;
  62. }
  63.  
  64. return idx;
  65. }
  66. int array[1000000];
  67.  
  68. void makeset(){
  69. for (int i=0; i<ct ;i++)
  70. array[i]=-1;
  71. }
  72.  
  73. int findpar(int x){
  74. int y=x,par;
  75. while (array[x] != -1) {
  76. x=array[x];
  77. }
  78.  
  79. par=x;
  80. int t;
  81. while (array[y] != -1) {
  82. t=y;
  83. y=array[y];
  84. array[t]=par;
  85. }
  86. return par;
  87. }
  88.  
  89. int mergeset(int x,int y){
  90.  
  91. if (findpar(x) != findpar(y)) {
  92. array[x]=y;
  93. return 1;
  94. }
  95. return 0;
  96. }
  97.  
  98. bool valid (node temp){
  99. if ( mergeset( mp[temp.s1] ,mp[temp.s2] ) ){
  100. if (temp.cost!=0)
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106. void kruskal(){
  107.  
  108. node temp;
  109.  
  110. while ( !pq.empty() ) {
  111. temp=pq.top();
  112. pq.pop();
  113. if ( valid( temp ) )
  114. cout << temp.id<<endl;
  115. }
  116.  
  117. }
  118.  
  119. int main() {
  120. int t;
  121. cin >>t;
  122.  
  123. while (t-- ){
  124.  
  125. int n,m;
  126. cin >>n>>m;
  127. node temp;
  128.  
  129. for (int i=0; i<n ;i++ ){
  130. cin>>temp.id>>temp.s1>>temp.s2;
  131.  
  132. hash(temp.s1);
  133. hash(temp.s2);
  134.  
  135. temp.cost=0;
  136. pq.push(temp);
  137. }
  138.  
  139. for (int i=0; i<m ;i++ ){
  140. cin>>temp.id>>temp.s1>>temp.s2>>temp.cost;
  141.  
  142. hash(temp.s1);
  143. hash(temp.s2);
  144.  
  145. pq.push(temp);
  146. }
  147. makeset();
  148. kruskal();
  149. }
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement