Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl "\n"
  4. const int maxn=1e5+5;
  5. struct road
  6. {
  7. int y,w;
  8. road (int _y,int _w)
  9. {
  10. y=_y; w=_w;
  11. }
  12. };
  13. struct euler
  14. {
  15. int depth, node;
  16. euler(){};
  17. euler(int _node,int _depth)
  18. {
  19. node=_node;
  20. depth=_depth;
  21. }
  22. bool operator <(euler v)const
  23. {
  24. return depth<v.depth;
  25. }
  26. };
  27. euler e[4*maxn];///build euler path
  28. euler sparse[32][4*maxn];///store sparse tables
  29. int n,cnt;
  30. int first_encounter[maxn],cost[maxn],paw[32];
  31. int par[maxn],l[maxn],r[maxn],max_length[maxn];
  32. stack<road>st;
  33. vector<pair<int, int> >v[maxn];
  34. ///----------------------------------------------
  35. void pow_pow()
  36. {
  37. int i=0;
  38. paw[0]=1;
  39. for(int i=1;i<32;i++)
  40. {
  41. paw[i]=paw[i-1]*2;
  42. }
  43. }
  44. ///----------------------------------------------
  45. void init_dsu()
  46. {
  47. for(int i=0;i<=n;i++)
  48. {
  49. par[i]=i;
  50. l[i]=i;
  51. r[i]=i;
  52. }
  53. }
  54. ///----------------------------------------------
  55. void read()
  56. {
  57. cin>>n;
  58. for(int i=0;i<n-1;i++)
  59. {
  60. int b,e,w;
  61. cin>>b>>e>>w;
  62. v[b].push_back(make_pair(e,w));
  63. v[e].push_back(make_pair(b,w));
  64. st.push(road(b,e));
  65. }
  66. }
  67. ///----------------------------------------------
  68. void dfs(int k,int t,int p)
  69. {
  70. first_encounter[k]=t;
  71. e[cnt++]=euler(k,t);
  72. for(int i=0;i<v[k].size();i++)
  73. {
  74. int nb=v[k][i].first;
  75. int c=v[k][i].second;
  76. if(nb!=p)
  77. {
  78. cost[nb]=cost[k]+c;
  79. dfs(nb,t+1,k);
  80. e[cnt++]=euler(k,t);
  81. }
  82. }
  83. }
  84. ///----------------------------------------------
  85. int precompute()
  86. {
  87. int root=0;
  88. for(int i=1;i<n;i++)
  89. {
  90. if(v[root].size()<v[i].size())
  91. {
  92. root=i;
  93. }
  94. }
  95. return root;
  96. }
  97. ///----------------------------------------------
  98. void build_sparse()
  99. {
  100. for(int i=0;i<cnt;i++)
  101. {
  102. sparse[0][i]=e[i];
  103. }
  104. for(int i=1;(1<<i)<cnt;i++)
  105. {
  106. for(int j=0;j+(1<<i)-1<cnt;j++)
  107. {
  108. sparse[i][j]=min(sparse[i-1][j],sparse[i-1][j+(1<<i-1)]);
  109. //cout<<sparse[i][j].depth<<endl;
  110. }
  111. }
  112. }
  113. ///----------------------------------------------
  114. int find_par(int i)
  115. {
  116. if(par[i]==i)return i;
  117. return par[i]=find_par(par[i]);
  118. }
  119. ///----------------------------------------------
  120. int LCA(int y,int w)
  121. {
  122. int f=first_encounter[y];
  123. int s=first_encounter[w];
  124. if(f>s)swap(f,s);
  125. int j=(int)log2(s-f+1);
  126.  
  127. return min(sparse[j][f],sparse[j][s-(1<<j)+1]).node;
  128. }
  129. ///----------------------------------------------
  130. int find_sum(int y,int w)
  131. {
  132. int an=LCA(y,w);
  133. //cout<<y<<" "<<w<<" "<<cost[y]<<" "<<cost[w]<<" "<<cost[an]<<endl;
  134. return (cost[y]-cost[an])+(cost[w]-cost[an]);
  135. }
  136. ///----------------------------------------------
  137. void compute_ans()
  138. {
  139. int y,w;
  140. stack<pair<int, int> >ans;
  141. while(!st.empty())
  142. {
  143. road nb=st.top();
  144. st.pop();
  145. y=nb.y; w=nb.w;
  146. int p1=find_par(y);
  147. int p2=find_par(w);
  148. if(max_length[p1]<max_length[p2])
  149. ans.push(make_pair(max_length[p1],max_length[p2]));
  150. else
  151. ans.push(make_pair(max_length[p2],max_length[p1]));
  152. if(p1!=p2)
  153. {
  154. cout<<"/**"<<endl;
  155. int sum1,sum2,sum3,sum4,sum5,sum6,xy;
  156. sum1=find_sum(l[p1],l[p2]);
  157. cout<<sum1<<" "<<l[p1]<<" "<<l[p2]<<endl;
  158. sum2=find_sum(l[p1],r[p2]);
  159. cout<<sum2<<" "<<l[p1]<<" "<<r[p2]<<endl;
  160. sum3=find_sum(r[p1],l[p2]) ;
  161. cout<<sum3<<" "<<r[p1]<<" "<<l[p2]<<endl;
  162. sum4=find_sum(r[p1],r[p2]);
  163. cout<<sum4<<" "<<r[p1]<<" "<<r[p2]<<endl;
  164. sum5=find_sum(l[p1],r[p1]);
  165. cout<<sum5<<" "<<l[p1]<<" "<<r[p1]<<endl;
  166. sum6=find_sum(l[p2],r[p2]);
  167. cout<<sum6<<" "<<l[p2]<<" "<<r[p2]<<endl;
  168.  
  169.  
  170. if(sum1>max_length[p1])
  171. {
  172. max_length[p1]=sum1;
  173. r[p1]=l[p2];
  174. }
  175. if(sum2>max_length[p1])
  176. {
  177. max_length[p1]=sum2;
  178. r[p1]=r[p2];
  179. }
  180. if(sum3>max_length[p1])
  181. {
  182. max_length[p1]=sum3;
  183. l[p1]=r[p1];
  184. r[p1]=l[p2];
  185. }
  186. if(sum4>max_length[p1])
  187. {
  188. max_length[p1]=sum4;
  189. l[p1]=r[p1];
  190. r[p1]=r[p2];
  191. //r[p1]=r[p2];
  192. }
  193. if(sum5>max_length[p1])
  194. {
  195. max_length[p1]=sum5;
  196. }
  197. if(sum6>max_length[p1])
  198. {
  199. max_length[p1]=sum6;
  200. r[p1]=r[p2];
  201. l[p1]=l[p2];
  202. }
  203. par[p2]=p1;
  204. }
  205. }
  206. while(!ans.empty())
  207. {
  208. cout<<ans.top().first<<" "<<ans.top().second<<endl;
  209. ans.pop();
  210. }
  211. }
  212. ///----------------------------------------------
  213. void solve()
  214. {
  215. init_dsu();
  216. pow_pow();
  217. int root=precompute();
  218. dfs(root,0,root);
  219. build_sparse();
  220. for(int i=0;i<4;i++)
  221. {
  222. for(int j=0;j<2*n;j++)
  223. {
  224. cout<<sparse[i][j].depth<<" ";
  225. }
  226. cout<<endl;
  227. }
  228. compute_ans();
  229. }
  230. ///----------------------------------------------
  231. int main()
  232. {
  233. ios_base::sync_with_stdio(0);
  234. cin.tie(0);
  235. read();
  236. solve();
  237. return 0;
  238. }
  239. /*
  240. 5
  241. 1 2 2
  242. 2 3 1
  243. 2 4 2
  244. 1 5 3
  245. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement