Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. /*
  2. */
  3.  
  4. #pragma comment(linker, "/STACK:16777216")
  5. #define _CRT_SECURE_NO_WARNINGS
  6.  
  7. #include <fstream>
  8. #include <iostream>
  9. #include <string>
  10. #include <complex>
  11. #include <math.h>
  12. #include <set>
  13. #include <vector>
  14. #include <map>
  15. #include <queue>
  16. #include <stdio.h>
  17. #include <stack>
  18. #include <algorithm>
  19. #include <list>
  20. #include <ctime>
  21. #include <memory.h>
  22. #include <assert.h>
  23.  
  24. #define y0 sdkfaslhagaklsldk
  25. #define y1 aasdfasdfasdf
  26. #define yn askfhwqriuperikldjk
  27. #define j1 assdgsdgasghsf
  28. #define tm sdfjahlfasfh
  29. #define lr asgasgash
  30. #define norm asdfasdgasdgsd
  31.  
  32. #define eps 1e-9
  33. #define M_PI 3.141592653589793
  34. #define bs 1000000007
  35. #define bsize 350
  36.  
  37. using namespace std;
  38.  
  39. const int INF = 1e9;
  40. const int N = 515000;
  41.  
  42. using namespace std;
  43.  
  44. int n, ar[N];
  45. int t[N];
  46. int used[N];
  47. long long res;
  48. vector<int> g[N];
  49.  
  50. int sum(int r)
  51. {
  52. int res = 0;
  53. for (; r >= 0; r = (r&(r + 1)) - 1)
  54. res += t[r];
  55. return res;
  56. }
  57.  
  58. int sum(int l,int r)
  59. {
  60. return sum(r) - sum(l - 1);
  61. }
  62.  
  63. void inc(int i, int delta)
  64. {
  65. for (; i <N; i = (i | (i + 1)))
  66. t[i] += delta;
  67. }
  68.  
  69. void dfs(int v)
  70. {
  71. used[v] = 1;
  72. res += sum(ar[v] - 1);
  73. inc(ar[v], 1);
  74. for (int i = 0; i < g[v].size(); i++)
  75. {
  76. int to = g[v][i];
  77. if (used[to])
  78. continue;
  79. dfs(to);
  80. }
  81. inc(ar[v], -1);
  82. }
  83.  
  84. int CNT;
  85. int done[N];
  86. int thold;
  87. long long ans;
  88. int ans_v;
  89. int total[N];
  90. vector<int> ent[N];
  91. map<pair<int, pair<int, int> >, int> mapp;
  92.  
  93. void func(int v)
  94. {
  95. done[v] = 1;
  96. if (ar[v] > thold)
  97. ++CNT;
  98. for (int i = 0; i < g[v].size(); i++)
  99. {
  100. int to = g[v][i];
  101. if (done[to])
  102. continue;
  103. func(to);
  104. }
  105. }
  106.  
  107. int count_sub(int v1, int v2, int val)
  108. {
  109. CNT = 0;
  110. thold = val;
  111. for (int i = 1; i <= n; i++)
  112. done[i] = 0;
  113. done[v1] = 1;
  114. done[v2] = 1;
  115. func(v2);
  116. return CNT;
  117. }
  118.  
  119. int smart_sub(int par, int son, int val)
  120. {
  121. if (mapp.find(make_pair(val, make_pair(par, son))) != mapp.end())
  122. return mapp[make_pair(val, make_pair(par, son))];
  123. return total[val + 1] - mapp[make_pair(val, make_pair(son, par))];
  124. }
  125.  
  126. void trace_solve(int v,long long res)
  127. {
  128. used[v] = 1;
  129. if (res < ans || (res == ans&&v < ans_v))
  130. {
  131. ans = res;
  132. ans_v = v;
  133. }
  134.  
  135. //cout << v << " " << res << endl;
  136. for (int i = 0; i < g[v].size(); i++)
  137. {
  138. int to = g[v][i];
  139. if (used[to])
  140. continue;
  141. long long new_res = res;
  142. new_res -= total[ar[v] + 1];
  143. new_res += smart_sub(to, v, ar[v]);// count_sub(to, v, ar[v]);
  144. new_res += total[ar[to] + 1];
  145. new_res -= smart_sub(v, to, ar[to]);// count_sub(v, to, ar[to]);
  146. trace_solve(to, new_res);
  147. }
  148. }
  149.  
  150. vector<pair<int,int> > queries[N];
  151.  
  152. void prec(int v)
  153. {
  154. used[v] = 1;
  155. for (int i = 0; i < g[v].size(); i++)
  156. {
  157. int to = g[v][i];
  158. if (used[to])
  159. continue;
  160. prec(to);
  161. queries[ar[v]].push_back(make_pair(v, to));
  162. queries[ar[to]].push_back(make_pair(v, to));
  163. }
  164. }
  165.  
  166. int tin[N], tout[N], timer;
  167.  
  168. void build_dfs(int v)
  169. {
  170. ++timer;
  171. tin[v] = timer;
  172. used[v] = 1;
  173. for (int i = 0; i < g[v].size(); i++)
  174. {
  175. int to = g[v][i];
  176. if (used[to])
  177. continue;
  178. build_dfs(to);
  179. }
  180. ++timer;
  181. tout[v] = timer;
  182. }
  183.  
  184. int main(){
  185. //freopen("fabro.in","r",stdin);
  186. //freopen("fabro.out","w",stdout);
  187. //freopen("F:/in.txt", "r", stdin);
  188. //freopen("F:/output.txt", "w", stdout);
  189. //ios_base::sync_with_stdio(0);
  190. //cin.tie(0);
  191.  
  192. cin >> n;
  193. for (int i = 1; i <= n; i++)
  194. {
  195. cin >> ar[i];
  196. total[ar[i]]++;
  197. ent[ar[i]].push_back(i);
  198. }
  199.  
  200. for (int i = 1; i < n; i++)
  201. {
  202. int a, b;
  203. cin >> a >> b;
  204. g[a].push_back(b);
  205. g[b].push_back(a);
  206. }
  207.  
  208. build_dfs(1);
  209.  
  210. for (int i = 0; i <N; i++)
  211. used[i] = 0;
  212.  
  213. prec(1);
  214.  
  215. for (int i = 1; i <N; i++)
  216. used[i] = 0;
  217.  
  218. for (int i = n; i; --i)
  219. {
  220. for (int j = 0; j < queries[i].size(); j++)
  221. {
  222. int vert = queries[i][j].second;
  223. //cout <<vert<<"%"<< sum(tin[vert], tout[vert]) << endl;
  224.  
  225. mapp[make_pair(i, make_pair(queries[i][j].first, queries[i][j].second))] = sum(tin[vert], tout[vert]);
  226. }
  227. for (int j = 0; j < ent[i].size(); j++)
  228. {
  229. int v = ent[i][j];
  230. //cout << v<<"@" << tin[v] << endl;
  231. inc(tin[v], 1);
  232. }
  233. }
  234.  
  235. for (int i = 0; i < N; i++)
  236. t[i] = used[i] = 0;
  237.  
  238. dfs(1);
  239.  
  240. for (int i = n; i; --i)
  241. total[i] += total[i + 1];
  242.  
  243. //cout << res << endl;
  244.  
  245. /*for (int i = n; i >=1; i--)
  246. {
  247. for (int j = 1; j <= n; j++)
  248. {
  249. used[j] = 0;
  250. }
  251. for (int j = 0; j <= n; j++)
  252. {
  253. t[j] = 0;
  254. }
  255. res = 0;
  256. dfs(i);
  257. cout << "!"<<i << " " << res << endl;
  258. }
  259. */
  260.  
  261. ans = res;
  262. ans_v = 1;
  263.  
  264. for (int i = 0; i <N; i++)
  265. used[i] = 0, t[i] = 0;
  266.  
  267. for (int i = 0; i <N; i++)
  268. used[i] = 0,
  269. t[i] = 0;
  270.  
  271. trace_solve(1,res);
  272.  
  273. cout << ans_v << " " << ans << endl;
  274.  
  275. cin.get(); cin.get();
  276. return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement