Guest User

Untitled

a guest
May 30th, 2012
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. //submit to: 1268 problem
  2. // CP.cpp : Defines the entry for the console application.
  3.  
  4. #include <iostream>
  5. #include <math.h>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <set>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. #define D 9973
  15.  
  16. #pragma comment(linker, "/STACK:167770000")
  17.  
  18.  
  19. int l[110000];
  20. int r[110000];
  21. int f[210000];
  22. int t[210000];
  23. int index[110000];
  24. int p[110000];
  25. int pc[110000] = {0};
  26. int pclus[110000];
  27. int pclusc[110000] = {0};
  28.  
  29. bool stop[110000] = {false};
  30. bool visit[110000] = {false};
  31. int n;
  32.  
  33. #define max(a,b) ((a)>(b)?(a):(b))
  34.  
  35. struct edge
  36. {
  37. int f;
  38. int t;
  39. bool operator < (const edge & e) const
  40. {
  41. return f < e.f || f == e.f && t < e.t;
  42. }
  43. };
  44.  
  45. vector<edge> vv;
  46.  
  47. int vsize;
  48.  
  49. int vcnt = 0;
  50. int porog = 0;
  51.  
  52. void setclusp(int a,int pset)
  53. {
  54. pclus[a] = pset;
  55. if (stop[a])
  56. return;
  57. int ndx = index[a];
  58. while (ndx < (int)vsize && f[ndx] == a)
  59. {
  60. int vt = t[ndx];
  61. if (vt != p[a])
  62. setclusp(vt,pset);
  63. ++ndx;
  64. }
  65. }
  66.  
  67. inline bool isparchi(int vp, int vc)
  68. {
  69. if (vp == -1)
  70. return true;
  71. return (l[vp] < r[vc] && r[vp] >= r[vc]);
  72. }
  73. int DFS(int a)
  74. {
  75. visit[a] = true;
  76. l[a] = vcnt;
  77.  
  78. int sum = 0;
  79. int ndx = index[a];
  80.  
  81. while (ndx < (int)vsize && f[ndx] == a)
  82. {
  83. int vt = t[ndx];
  84. if (!visit[vt])
  85. sum += DFS(vt);
  86. else
  87. p[a] = vt;
  88. ++ndx;
  89. }
  90.  
  91. if (sum >= porog)
  92. {
  93. sum = 0;
  94. setclusp(a,a);
  95. stop[a] = true;
  96. }
  97. ++sum;
  98. ++vcnt;
  99. r[a] = vcnt;
  100. return sum;
  101. }
  102. int cm = 0;
  103. void upd(int vu)
  104. {
  105. pclusc[vu] = max(cm, pclusc[vu]);
  106.  
  107. if (stop[vu])
  108. return;
  109.  
  110. int ndx = index[vu];
  111. while (ndx < (int)vsize && f[ndx] == vu)
  112. {
  113. int vt = t[ndx];
  114. if (vt != p[vu])
  115. {
  116. upd(vt);
  117. }
  118. ++ndx;
  119. }
  120.  
  121. }
  122. int get(int vf, int vt)
  123. {
  124. int res = 0;
  125. int cur = vf;
  126. for (;;)
  127. {
  128. int curn = pclus[cur];
  129. if (isparchi(curn, vt))
  130. break;
  131. res = max(res,pclusc[cur]);
  132. if (cur == curn)
  133. throw 0;
  134. if (curn == -1)
  135. throw 0;
  136. cur = curn;
  137. }
  138. int cnt = 0;
  139. while (!isparchi(cur, vt))
  140. {
  141. res = max(res,pc[cur]);
  142. cur = p[cur];
  143. ++cnt;
  144. if (cnt > 10000)
  145. throw 0;
  146. }
  147. res = max(res,pc[cur]);
  148. return res;
  149. }
  150. int main()
  151. {
  152. /* ifstream cin("inp.txt");
  153. ofstream cout("out.txt");
  154.  
  155. n = 100000;
  156. cout<<n<<'\n';
  157. for (int i=1;i<n;i++)
  158. cout<<i<<' '<<i+1<<'\n';
  159. cout<<n;
  160. for (int i=0;i<n;i++)
  161. cout<<"I 100000 1\n";
  162. return 0;
  163. */
  164. cin>>n;
  165. for (int i=0;i<n-1;i++)
  166. {
  167. edge a;
  168. // cin>>a.f>>a.t;
  169. scanf("%d%d",&a.f,&a.t);
  170. vv.push_back(a);
  171. swap(a.f,a.t);
  172. vv.push_back(a);
  173. f[i+1] = a.f;
  174. t[i+1] = a.t;
  175. }
  176. sort(vv.begin(),vv.end());
  177. vsize = vv.size();
  178. for (int i=0;i<vsize;i++)
  179. {
  180. f[i] = vv[i].f;
  181. t[i] = vv[i].t;
  182. }
  183.  
  184. int ndx = 0;
  185. for (int i=1;i<=n;i++)
  186. {
  187. index[i] = ndx;
  188. while (ndx < (int)vsize && f[ndx] == i)
  189. ++ndx;
  190. }
  191.  
  192. porog = 250;
  193. DFS(1);
  194. setclusp(1,1);
  195. pclus[1] = -1;
  196. p[1] = -1;
  197. stop[1] = true;
  198.  
  199. int m;cin>>m;
  200. for (int aaa = 0;aaa<m;aaa++)
  201. {
  202. char c[2];
  203. scanf("%s",c);
  204. // cin>>c;
  205. if (c[0] == 'I')
  206. {
  207. int e,val;
  208. // cin>>e>>val;
  209. scanf("%d%d",&e,&val);
  210.  
  211. pc[e] += val;
  212. if (pclus[e] != -1)
  213. {
  214. cm = pc[e];
  215. upd(e);
  216. }
  217. }
  218. if (c[0] == 'G')
  219. {
  220. int vf,vt;
  221. // cin>>vf>>vt;
  222. scanf("%d%d",&vf,&vt);
  223. cout<<max(get(vf,vt),get(vt,vf))<<'\n';
  224. }
  225. }
  226.  
  227.  
  228.  
  229. return 0;
  230. }
Add Comment
Please, Sign In to add comment