dfs序+树状数组

参考这里

代码:

bzoj1103

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 250010
using namespace std;

struct edge{
    int to,next;
}p[Maxn];
int n;
int head[Maxn],st[Maxn],l[Maxn],r[Maxn],c[Maxn<<1];
int tot,top,t;
void addedge(int a,int b){
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
}
inline 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;
}
void dfs(int u){
    st[++top]=u;
    while(top){
        int v=st[top];
        if(!l[v]){
            l[v]=++t;
            for(int i=head[v];i!=-1;i=p[i].next)
                st[++top]=p[i].to;
        }
        else{
            r[v]=++t;
            top--;
        }
    }
}
int lowbit(int x){
    return x&-x;
}
void update(int x,int d){
    while(x<=n+n){
        c[x]+=d;
        x+=lowbit(x);
    }
}
int sum(int x){
    int res=-1;
    while(x>0){
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
int main()
{
    int m,a,b;
    char s[5];
    memset(head,-1,sizeof head);
    n=read();
    for(int i=1;i<n;i++){
        a=read();b=read();
        if(a>b) swap(a,b);
        addedge(a,b);
    }
    dfs(1);
    for(int i=1;i<=n;i++){
        update(l[i],1);
        update(r[i],-1);
    }
    m=read();
    for(int i=0;i<n+m-1;i++){
        scanf("%s",s);
        if(s[0]=='A'){
            a=read();b=read();
            if(a>b) swap(a,b);
            update(l[b],-1);
            update(r[b],1);
        }
        else{
            a=read();
            //cout<<sum(l[a])<<endl;
            printf("%d\n",sum(l[a]));
        }
    }
	return 0;
}

hdu3887

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 200010
using namespace std;

struct edge{
    int to,next;
}p[Maxn];
int head[Maxn],st[Maxn],l[Maxn],r[Maxn],fa[Maxn],c[Maxn],ans[Maxn];
int tot,top,t,n;
void addedge(int a,int b){
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
    p[tot].to=a;
    p[tot].next=head[b];
    head[b]=tot++;
}
void dfs(int u){
    st[++top]=u;
    fa[u]=-1;
    while(top){
        int v=st[top];
        if(!l[v]){
            l[v]=++t;
            for(int i=head[v];i!=-1;i=p[i].next)
                if(p[i].to!=fa[v]){
                    st[++top]=p[i].to;
                    fa[p[i].to]=v;
                }
        }
        else{
            r[v]=++t;
            top--;
        }
    }
}
int lowbit(int x){
    return x&-x;
}
int ask(int x){
    int res=0;
    for(int i=x;i>0;i-=lowbit(i))
        res+=c[i];
    return res;
}
void update(int x,int d){
    while(x<=n+n){
        c[x]+=d;
        x+=lowbit(x);
    }
}
int main()
{
    int u,a,b;
    while(cin>>n>>u,n){
        tot=top=t=0;
        memset(l,0,sizeof l);
        //memset(r,0,sizeof r);
        memset(head,-1,sizeof head);
        for(int i=1;i<n;i++){
            cin>>a>>b;
            addedge(a,b);
        }
        //cout<<"****"<<endl;
        dfs(u);
        //cout<<"*****"<<endl;
        for(int i=1;i<=n+n;i++)
            update(i,1);
        for(int i=n;i>0;i--){
            ans[i]=(ask(r[i]-1)-ask(l[i]))/2;
            update(l[i],-1);
            update(r[i],-1);
        }
        printf("%d",ans[1]);
        for(int i=2;i<=n;i++)
            printf(" %d",ans[i]);
        puts("");
    }
	return 0;
}

poj3321

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 200010
using namespace std;

struct edge{
    int to,next;
}p[Maxn];
int head[Maxn],l[Maxn],r[Maxn],c[Maxn],st[Maxn],fa[Maxn],vis[Maxn];
int tot,n,top,t;
void addedge(int a,int b){
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
    p[tot].to=a;
    p[tot].next=head[b];
    head[b]=tot++;
}
void dfs(){
    st[++top]=1;
    fa[1]=-1;
    while(top){
        int v=st[top];
        if(!l[v]){
            l[v]=++t;
            for(int i=head[v];i!=-1;i=p[i].next)
                if(fa[v]!=p[i].to){
                    st[++top]=p[i].to;
                    fa[p[i].to]=v;
                }
        }
        else{
            r[v]=++t;
            top--;
        }
    }
}
int lowbit(int x){
    return x&-x;
}
int ask(int x){
    int res=0;
    while(x>0){
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
void update(int x,int d){
    while(x<=n+n){
        c[x]+=d;
        x+=lowbit(x);
    }
}
int main()
{
    int m,x,d,a,b;
    char s[2];
    while(cin>>n){
        tot=top=t=0;
        memset(head,-1,sizeof head);
        memset(vis,0,sizeof vis);
        memset(l,0,sizeof l);
        for(int i=1;i<n;i++){
            cin>>a>>b;
            addedge(a,b);
        }
        dfs();
        for(int i=1;i<=n+n;i++)
            update(i,1);
        cin>>m;
        while(m--){
            scanf("%s%d",s,&x);
            if(s[0]=='C'){
                if(!vis[x]) {d=-1;vis[x]=1;}
                else {d=1;vis[x]=0;}
                update(l[x],d);
                update(r[x],d);
            }
            else printf("%d\n",(ask(r[x])-ask(l[x]-1))/2);
        }
    }
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值