您的位置:首页 > 其它

【bzoj1103】【POI2007】【大都市】【meg】【人工栈】

2016-08-17 22:46 465 查看

题目大意

一棵有根树,每次修改一条边或询问跟到一个点的路径上有多少条边没被修改。

解题思路

可以用括号序表示树上的一条路径,访问时间戳+1,离开时间戳-1,修改一条边就是将深度大的点的时间戳都改为0,表示对后面没影响。最恶心的是要打人工栈。

code

#include<set>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const maxn=250000,maxa=1000000000;
int n,m,gra,time,top,to[maxn+10],next[maxn+10],begin[maxn+10],dfn[maxn+10],lea[maxn+10],st[maxn+10],t[maxn*2+10];
void insert(int u,int v){
to[++gra]=v;
next[gra]=begin[u];
begin[u]=gra;
}
void dfs(int s){
st[++top]=s;
for(;top;){
if(begin[st[top]])st[top+1]=to[begin[st[top]]],begin[st[top]]=next[begin[st[top]]],top++,dfn[st[top]]=++time;
else lea[st[top]]=++time,top--;
}
}
void change(int p,int v){
for(;p<=time;){
t[p]+=v;
p+=p&(-p);
}
}
int qury(int p){
int ans=0;
for(;p>=1;){
ans+=t[p];
p-=p&(-p);
}
return ans;
}
int main(){
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%d",&n);
fo(i,1,n-1){
int u,v;scanf("%d%d",&u,&v);
if(u>v)swap(u,v);
insert(u,v);
}
dfs(1);
fo(i,2,n){
change(dfn[i],1);
change(lea[i],-1);
}
scanf("%d\n",&m);
fo(i,1,n+m-1){
char ch=getchar();int a,b;
if(ch=='W'){
scanf("%d\n",&a);
printf("%d\n",qury(dfn[a]));
}
else{
scanf("%d%d\n",&a,&b);
change(dfn[b],-1);
change(lea[b],1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: