out_of_theblue10

Untitled

Mar 10th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <vector>
  4. #define N 200010
  5. #define mp make_pair
  6. #define f first
  7. #define s second
  8. using namespace std;
  9.  
  10. int Mc[N], Mx[N], My[N], ind[N], inq[N], P[N], n, m, q, x, y, c, nod, k, total, nr[N], Q[N];
  11. vector<pair<int, int> > :: iterator it;
  12. vector<pair<int, int> > v[N], u;
  13. bool ok;
  14.  
  15. bool cmp(int x, int y)
  16. {
  17. return Mc[x] > Mc[y];
  18. }
  19.  
  20. bool cmp2(pair<int, int> x, pair<int, int> y)
  21. {
  22. return x.f < y.f;
  23. }
  24.  
  25. void merge_vectors(vector<pair<int, int> > v1, vector<pair<int, int> > v2)
  26. {
  27. int i, j, n1, n2;
  28. n1 = v1.size();
  29. n2 = v2.size();
  30. for(i = 0, j = 0; i < n1 or j < n2; )
  31. {
  32. if(i < n1 and i < n2 and v1[i].f < v2[j].f) u.push_back(v1[i]), i++; else
  33. if(i < n1 and i < n2 and v1[i].f >= v2[j].f) u.push_back(v2[j]), j++; else
  34. if(i < n1) u.push_back(v1[i]), i++; else u.push_back(v2[j]), j++;
  35. }
  36. }
  37.  
  38. int comp(int x)
  39. {
  40. if(P[x] == x) return x;
  41. return P[x] = comp(P[x]);
  42. }
  43.  
  44. void reunite(int x, int y)
  45. {
  46. P[comp(x)] = comp(y);
  47. }
  48.  
  49. int main()
  50. {
  51. int i;
  52. ifstream fi("luff.in");
  53. ofstream fo("luff.out");
  54. fi >> n >> m >> q;
  55. for(i = 1; i <= m; i++)
  56. {
  57. fi >> x >> y >> c;
  58. Mx[i] = x; My[i] = y; Mc[i] = c; ind[i] = i;
  59. }
  60. for(i = 1; i <= q; i++)
  61. {
  62. fi >> nod >> k;
  63. inq[nod] = 1;
  64. v[nod].push_back(mp(k, i));
  65. }
  66. for(i = 1; i <= n; i++) sort(v[i].begin(), v[i].end(), cmp2);
  67. for(i = 1; i <= n; i++)
  68. {
  69. P[i] = i;
  70. nr[i] = 1;
  71. }
  72. sort(ind+1, ind+m+1, cmp);
  73. for(i = 1; i <= m; i++)
  74. if(comp(Mx[ind[i]]) != comp(My[ind[i]]))
  75. {
  76. x = Mx[ind[i]]; y = My[ind[i]];
  77. merge_vectors(v[comp(x)], v[comp(y)]);
  78. total = nr[comp(x)] + nr[comp(y)];
  79. ok = 0;
  80. if(inq[comp(x)] or inq[comp(y)]) ok = 1;
  81. reunite(x, y);
  82. c = comp(x);
  83. if(ok)
  84. {
  85. inq[x] = inq[y] = 0;
  86. inq[c] = 1;
  87. }
  88. nr[c] = total;
  89. v[c] = u;
  90. for(it = v[c].begin(); it != v[c].end();)
  91. if(it->f <= nr[c])
  92. {
  93. ++it;
  94. Q[it->s] = Mc[ind[i]];
  95. v[c].erase(v[c].begin());
  96. } else ++it;
  97. //verific daca nu cumva am indeplinit un query
  98. }
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment