Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //submit to: 1268 problem
- // CP.cpp : Defines the entry for the console application.
- #include <iostream>
- #include <math.h>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <fstream>
- using namespace std;
- #define D 9973
- #pragma comment(linker, "/STACK:167770000")
- int l[110000];
- int r[110000];
- int f[210000];
- int t[210000];
- int index[110000];
- int p[110000];
- int pc[110000] = {0};
- int pclus[110000];
- int pclusc[110000] = {0};
- bool stop[110000] = {false};
- bool visit[110000] = {false};
- int n;
- #define max(a,b) ((a)>(b)?(a):(b))
- struct edge
- {
- int f;
- int t;
- bool operator < (const edge & e) const
- {
- return f < e.f || f == e.f && t < e.t;
- }
- };
- vector<edge> vv;
- int vsize;
- int vcnt = 0;
- int porog = 0;
- void setclusp(int a,int pset)
- {
- pclus[a] = pset;
- if (stop[a])
- return;
- int ndx = index[a];
- while (ndx < (int)vsize && f[ndx] == a)
- {
- int vt = t[ndx];
- if (vt != p[a])
- setclusp(vt,pset);
- ++ndx;
- }
- }
- inline bool isparchi(int vp, int vc)
- {
- if (vp == -1)
- return true;
- return (l[vp] < r[vc] && r[vp] >= r[vc]);
- }
- int DFS(int a)
- {
- visit[a] = true;
- l[a] = vcnt;
- int sum = 0;
- int ndx = index[a];
- while (ndx < (int)vsize && f[ndx] == a)
- {
- int vt = t[ndx];
- if (!visit[vt])
- sum += DFS(vt);
- else
- p[a] = vt;
- ++ndx;
- }
- if (sum >= porog)
- {
- sum = 0;
- setclusp(a,a);
- stop[a] = true;
- }
- ++sum;
- ++vcnt;
- r[a] = vcnt;
- return sum;
- }
- int cm = 0;
- void upd(int vu)
- {
- pclusc[vu] = max(cm, pclusc[vu]);
- if (stop[vu])
- return;
- int ndx = index[vu];
- while (ndx < (int)vsize && f[ndx] == vu)
- {
- int vt = t[ndx];
- if (vt != p[vu])
- {
- upd(vt);
- }
- ++ndx;
- }
- }
- int get(int vf, int vt)
- {
- int res = 0;
- int cur = vf;
- for (;;)
- {
- int curn = pclus[cur];
- if (isparchi(curn, vt))
- break;
- res = max(res,pclusc[cur]);
- if (cur == curn)
- throw 0;
- if (curn == -1)
- throw 0;
- cur = curn;
- }
- int cnt = 0;
- while (!isparchi(cur, vt))
- {
- res = max(res,pc[cur]);
- cur = p[cur];
- ++cnt;
- if (cnt > 10000)
- throw 0;
- }
- res = max(res,pc[cur]);
- return res;
- }
- int main()
- {
- /* ifstream cin("inp.txt");
- ofstream cout("out.txt");
- n = 100000;
- cout<<n<<'\n';
- for (int i=1;i<n;i++)
- cout<<i<<' '<<i+1<<'\n';
- cout<<n;
- for (int i=0;i<n;i++)
- cout<<"I 100000 1\n";
- return 0;
- */
- cin>>n;
- for (int i=0;i<n-1;i++)
- {
- edge a;
- // cin>>a.f>>a.t;
- scanf("%d%d",&a.f,&a.t);
- vv.push_back(a);
- swap(a.f,a.t);
- vv.push_back(a);
- f[i+1] = a.f;
- t[i+1] = a.t;
- }
- sort(vv.begin(),vv.end());
- vsize = vv.size();
- for (int i=0;i<vsize;i++)
- {
- f[i] = vv[i].f;
- t[i] = vv[i].t;
- }
- int ndx = 0;
- for (int i=1;i<=n;i++)
- {
- index[i] = ndx;
- while (ndx < (int)vsize && f[ndx] == i)
- ++ndx;
- }
- porog = 250;
- DFS(1);
- setclusp(1,1);
- pclus[1] = -1;
- p[1] = -1;
- stop[1] = true;
- int m;cin>>m;
- for (int aaa = 0;aaa<m;aaa++)
- {
- char c[2];
- scanf("%s",c);
- // cin>>c;
- if (c[0] == 'I')
- {
- int e,val;
- // cin>>e>>val;
- scanf("%d%d",&e,&val);
- pc[e] += val;
- if (pclus[e] != -1)
- {
- cm = pc[e];
- upd(e);
- }
- }
- if (c[0] == 'G')
- {
- int vf,vt;
- // cin>>vf>>vt;
- scanf("%d%d",&vf,&vt);
- cout<<max(get(vf,vt),get(vt,vf))<<'\n';
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment