Advertisement
Guest User

Untitled

a guest
Sep 27th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. /*
  2. */
  3.  
  4. //#pragma comment(linker, "/STACK:16777216")
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <complex>
  9. #include <math.h>
  10. #include <set>
  11. #include <vector>
  12. #include <map>
  13. #include <queue>
  14. #include <stdio.h>
  15. #include <stack>
  16. #include <algorithm>
  17. #include <list>
  18. #include <ctime>
  19. #include <memory.h>
  20. #include <ctime>
  21. #include <assert.h>
  22.  
  23. #define y0 sdkfaslhagaklsldk
  24. #define y1 aasdfasdfasdf
  25. #define yn askfhwqriuperikldjk
  26. #define j1 assdgsdgasghsf
  27. #define tm sdfjahlfasfh
  28. #define lr asgasgash
  29.  
  30. #define eps 1e-8
  31. #define M_PI 3.141592653589793
  32. #define bs 1000000007
  33. #define bsize 512
  34.  
  35. using namespace std;
  36.  
  37. int n,prod[1<<20];
  38. vector<int> emp[1<<20];
  39. int m;
  40. vector<int> g[1<<20];
  41. int bound[1<<20];
  42. int val[1<<20];
  43. long long ans;
  44.  
  45. int set_emp(int id)
  46. {
  47. int res=bound[id];
  48. for (int i=0;i<g[id].size();i++)
  49. {
  50. int to=g[id][i];
  51. res=max(res,bound[to]);
  52. }
  53. return res;
  54. }
  55.  
  56. void update1(int id)
  57. {
  58. bound[id]=max(bound[id],val[id]);
  59. for(int i=0;i<g[id].size();i++)
  60. {
  61. int to=g[id][i];
  62. bound[to]=max(bound[to],val[id]);
  63. }
  64. }
  65.  
  66. void update_emp(int id)
  67. {
  68. bound[id]=max(bound[id],val[id]);
  69. for(int i=0;i<g[id].size();i++)
  70. {
  71. int to=g[id][i];
  72. bound[to]=max(bound[to],val[id]+1);
  73. }
  74. }
  75.  
  76. int main(){
  77. //freopen("beavers.in","r",stdin);
  78. //freopen("beavers.out","w",stdout);
  79. //freopen("F:/in.txt","r",stdin);
  80. //freopen("F:/output.txt","w",stdout);
  81. ios_base::sync_with_stdio(0);
  82. //cin.tie(0);
  83.  
  84. cin>>n;
  85. for (int i=1;i<=n;i++)
  86. {
  87. cin>>prod[i];
  88.  
  89. assert(prod[i]>=1&&prod[i]<=100000);
  90.  
  91. emp[prod[i]].push_back(i);
  92. }
  93.  
  94. cin>>m;
  95. for (int i=1;i<=m;i++)
  96. {
  97. int a,b;
  98. cin>>a>>b;
  99. g[a].push_back(b);
  100. g[b].push_back(a);
  101. }
  102.  
  103. for (int i=1;i<=n;i++)
  104. bound[i]=1;
  105.  
  106. for (int i=1;i<=100000;i++)
  107. random_shuffle(emp[i].begin(),emp[i].end());
  108.  
  109. for (int i=1;i<=100000;i++)
  110. {
  111. for (int j=0;j<emp[i].size();j++)
  112. {
  113. int id=emp[i][j];
  114. int cost=set_emp(id);
  115. ans+=cost;
  116. val[id]=cost;
  117. update1(id);
  118. }
  119.  
  120. while (true)
  121. {
  122. int changes=0;
  123. for (int j=0;j<emp[i].size();j++)
  124. {
  125. int id=emp[i][j];
  126. int cost=set_emp(id);
  127. if (val[id]<cost)
  128. {
  129. ans+=cost-val[id];
  130. val[id]=cost;
  131. changes=1;
  132. update1(id);
  133. }
  134. }
  135. if (changes==0)
  136. break;
  137. }
  138.  
  139. for (int j=0;j<emp[i].size();j++)
  140. {
  141. int id=emp[i][j];
  142. update_emp(id);
  143. }
  144. }
  145.  
  146. cout<<ans<<endl;
  147. //cin.get();cin.get();
  148. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement