bzoj1103: [POI2007]大都市meg
2016-04-30 09:34
351 查看
dfs序+树状数组维护;
#include<cstdio> #include<algorithm> #define rep(i,k,n) for(int i=k;i<=(n);i++) using namespace std; int read(){ int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } const int N=250005; struct E{ int to,next;E(int to=0,int next=0):to(to),next(next){} }edge[2*N]; int head ,tot=0,n,l ,r ,m,tr[2*N],sta[2*N],top=0,cc ,clock=0; void add(int x,int y){ edge[++tot]=E(y,head[x]);head[x]=tot; edge[++tot]=E(x,head[y]);head[y]=tot; } void Add(int x,int d){for(;x<=clock;x+=(x&-x))tr[x]+=d;} int Q(int x){int res=0;for(;x;x-=(x&-x))res+=tr[x];return res; } void dfs(){ sta[++top]=1; while(top){ int u=sta[top];if(!l[u])l[u]=++clock; for(int i=head[u];i;i=edge[i].next){ int v=edge[i].to; if(!l[v]){ sta[++top]=v;break; } }if(sta[top]==u){ top--;r[u]=++clock; } } } int main(){//freopen("in.in","r",stdin); n=read();int x,y; rep(i,1,n-1){ x=read();y=read();add(x,y); }dfs(); rep(i,2,n)Add(l[i],1),Add(r[i],-1); m=read();char s[20]; rep(i,1,n+m-1){ scanf("%s",s); if(s[0]=='A'){ x=read(),y=read(); Add(l[y],-1),Add(r[y],1); } else{x=read();printf("%d\n",Q(l[x]));} } }
相关文章推荐
- 2326: [HNOI2011]数学作业
- B. Crossed ladders
- git/github初级运用自如 (good)
- 根据二叉树的前序和中序遍历结果,还原二叉树
- iOS - 生成二维码
- Irrlicht学习笔记(3)--CustomSceneNode
- android sdk 下各个文件夹的作用
- 算法练习3.Longest Substring Without Repeating Characters 最长不重复子字符串(map)
- 个人冲刺10
- redis基础
- 宏定义中的"#"、"##"、"..."、"__VA_AGRS__"
- 1002. 写出这个数 (20)
- 第 15 章 CSS 文本样式[下]
- bzoj4310: 跳蚤
- POJ1200 Crazy Search(hash)
- 博客莫名被关,还好及时解决
- ADT之练习题
- 创业分享:创业的过程就是坚持的过程
- tomcat环境变量配置
- 广告行业知识