1103: [POI2007] Metropolitan Meg Time Limit:10 Sec Memory limit:162 MB
submit:2324 solved:1231
[Submit] [Status] [Discuss] Description
Under the influence of the economic globalization, the postman Blue Mary, who was accustomed to strolling in the early morning Country Road, began to deliver the mail on her motorbike.
However, she often recalls her previous stroll in the country. In the old days, the countryside numbered 1 sequentially. n small villages, some villages between some of the two
To the dirt road. From every village there is exactly one path to the village 1 (that is, Fort bitcoin). And, for every village, it goes to the path of the fort which happens to
Only after numbering the village numbered smaller than it. In addition, for all roads, they are not met in places other than villages. In this is not open
Place, there has never been a viaduct or an underground railroad. Over time, more and more dirt roads have been transformed into highways. So far, Blue Mary
It was also clear that the last dirt road had been transformed into a road scene. Now, there is no dirt road--all roads are roads, and old
Village has become a big city. Blue Mary remembered her letter-sending experience during the transformation. She had to go to a village from Fort.
And during the interval of two messengers, some of the dirt roads were transformed into highways. Now blue Mary needs your help: figure out every time she takes a messenger.
The number of dirt roads to go through. (On the road, she can ride a motorbike; for dirt roads, she has to push the cart.) ) Input
The first line is a number n (1 < = N < = 2 50000). The following n-1 rows, two integers per row a,b (1 < = a one line below contains an integer m
(1 < = m < = 2 50000), which means that blue Mary once sent a letter to M during the transformation. The following n+m-1 lines, with several information in two formats per line
, indicating a chronological occurrence of n+m-1 events: If this act a a B (a if this behavior W A, it means that blue Mary once sent a messenger from Fort
Village A. Output
There is a line of M, each containing an integer representing the number of dirt roads that were passed when a messenger was sent. Sample Input 5
1 2
1 3
1 4
4 5
4
W 5
A 1 4
W 5
A 4 5
W 5
W 2
A 1 2
A 1 3
Sample Output 2
1
0
1 HINT
originally thought that the tree chain dissection is over, finish the Dfs order of the puzzle. take the time to mend
#include <bits/stdc++.h> using namespace std;
const int MAXN = 250050;
int h[maxn],nx[maxn<<1],to[maxn<<1],len;
int fa[maxn],top[maxn],sz[maxn],ch[maxn],rk[maxn],dep[maxn],cnt;
int val[maxn],seg[maxn<<2];
int n,m;
void Insert (int a,int b) {Len++;nx[len]=h[a];to[len]=b;h[a]=len;
Len++;nx[len]=h[b];to[len]=a;h[b]=len; } void Pushup (int rt) {seg[rt]=seg[rt<<1]+seg[rt<<1|1];} void build (int rt,int l,int R) {if (l==r) {s Eg[rt]=1;if (l==1) {seg[rt]=0;}
return;}
int mid= (L+R) >>1;
Build (Rt<<1,l,mid);
Build (Rt<<1|1,mid+1,r);
Pushup (RT);
int query (int rt,int l,int r,int l,int R) {if (l<=l && r>=r) return SEG[RT];
int mid= (L+R) >>1;
if (r<=mid) return query (RT<<1,L,MID,L,R);
else if (l>mid) return query (RT<<1|1,MID+1,R,L,R);
return query (RT<<1,L,MID,L,MID) + query (RT<<1|1,MID+1,R,MID+1,R); } void Modify (int rt,int l,int r,int loc,int val) {intMid= (l+r) >>1;
if (l==r) {seg[rt]=0; return;}
if (loc<=mid) modify (Rt<<1,l,mid,loc,val);
else modify (Rt<<1|1,mid+1,r,loc,val);
Pushup (RT);
int LCA (int a,int b) {while (Top[a]!= top[b]) {//cout<<a<< "" <<b<<endl; Dep[top[a]] > dep[top[b]]?
A=fa[top[a]]: b=fa[top[b]]; return Dep[a] < Dep[b]?
A:B;
} void dfs1 (int nd,int pr) {FA[ND]=PR;
dep[nd]=dep[pr]+1;
Sz[nd]=1;
for (int i=h[nd];i;i=nx[i]) {if (To[i]!=fa[nd]) {DFS1 (to[i],nd);
Sz[nd]+=sz[to[i]];
if (Sz[to[i]] > Sz[ch[nd]]) {ch[nd]=to[i];
}} void dfs2 (int nd,int tp) {TOP[ND]=TP;
rk[nd]=++cnt;
if (ch[nd]!=0) DFS2 (CH[ND],TP); for (int i=h[nd];i;i=nx[i]) {if (To[i]!=fa[nd] && to[i]!=ch[nd]) {DFS2 (to[i],to[i)
); int Getans (int nd) {inT ret=0;
while (top[nd]!=1) {ret+=query (1,1,cnt,rk[top[nd]],rk[nd]);
ND=FA[TOP[ND]];
}//cout<<rk[nd]<< "" <<rk[top[nd]]<< "" <<ret<<endl;
Ret+=query (1,1,cnt,rk[top[nd]],rk[nd]);
return ret;
int main () {scanf ("%d", &n);
for (int i=1;i<=n-1;i++) {int _a,_b;
scanf ("%d%d", &_a,&_b);
Insert (_a,_b);
} DFS1 (1,0);
DFS2 (1,1);
Build (1,1,CNT);
scanf ("%d", &m);
for (int i=1;i<=n+m-1;i++) {char _c[60];int _a,_b;
scanf ("%s", _c);
if (_c[0]== ' W ') {scanf ("%d", &_a);
printf ("%d\n", Getans (_a));
} if (_c[0]== ' A ') {scanf ("%d%d", &_a,&_b);
Modify (1,1,cnt,rk[_b],0);
} return 0; }