/ Randle /

记录详情

Accepted

/in/foo.cc: In function 'int main()':
/in/foo.cc:63:9: warning: unused variable 's' [-Wunused-variable]
     int s = 1;
         ^
# 状态 耗时 内存占用
#1 Accepted 224ms 15.129 MiB
#2 Accepted 276ms 15.758 MiB
#3 Accepted 305ms 15.848 MiB
#4 Accepted 347ms 16.074 MiB
#5 Accepted 337ms 15.949 MiB
#6 Accepted 423ms 16.094 MiB
#7 Accepted 434ms 16.332 MiB
#8 Accepted 427ms 16.199 MiB
#9 Accepted 349ms 16.492 MiB

代码

#include <stdio.h>
#include <iostream>
using namespace std;
const int maxn = 3e5+4;
int c[maxn+5],n,m;
int size[maxn],top[maxn],son[maxn],tin[maxn],tout[maxn],cloc,fat[maxn],dep[maxn],que[maxn],q;
inline int lowbit(int x){return x&(-x);}
inline int sum(int x){int ret = 0;while(x){ret+=c[x];x-=lowbit(x);}return ret;}
inline void inc(int x,int a){while(x<=(cloc)+5){c[x]+=a;x+=lowbit(x);}}
int head[maxn],nex[maxn*2],dist[maxn*2],qq;
inline void connect(int a,int b){nex[++qq] = head[a];dist[qq] = b;head[a] = qq;nex[++qq] = head[b];dist[qq] = a;head[b] = qq;}
void dfs(int a,int fa){
    dep[a] = dep[fa]+1;
    fat[a] = fa;
    tin[a] = ++cloc;
    son[a] = -1;
    for(int i = head[a];i;i=nex[i]){
        int to = dist[i];
        if(to!=fa){
            dfs(to,a);
            size[a]+=size[to];
            if(son[a]==-1||size[to]>size[son[a]]){
                son[a] = to;
            }
        }
    }
    tout[a]= cloc+1;
}
void dfs2(int a,int t){
    top[a] = t;
    if(son[a]==-1) return;
    dfs2(son[a],t);
     for(int i = head[a];i;i=nex[i]){
        int to = dist[i];
        if(to!=fat[a]&&to!=son[a]){
            dfs2(to,to);
        }
    }
}
int lca(int x,int y){
    while(top[x]!=top[y]){
        if(dep[top[x]]>dep[top[y]])
            x = fat[top[x]];
        else
            y = fat[top[y]];
    }
    return dep[x]<dep[y]?x:y;
}
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;   
}  
int main(){
    n = read(),m = read();
    int s = 1;
    for(int i = 1;i<=n-1;i++){
        int x,y;x=read(),y=read();
        connect(x,y);
    }
    dfs(n/2,n/2);
    dfs2(n/2,n/2);
    for(int i = 1;i<=m;i++){
        char c;cin>>c;
        if(c=='Q'){
            int x=read(),y=read();
            if(!(sum(tin[x])+sum(tin[y])-2*sum(tin[lca(x,y)]))){
                puts("Yes");
            }else{
                puts("No");
            }
        }else if(c=='C'){
            int x=read(),y=read();
            if(dep[x]<dep[y]) x = y;
            inc(tin[x],1);inc(tout[x],-1);
            que[++q] = x;
        }else{
            int x=read();
            inc(tin[que[x]],-1);
            inc(tout[que[x]],1);
        }
    }
    return 0;
}

信息

递交者
类型
递交
题目
部落冲突(数据加强)
题目数据
下载
语言
C++
递交时间
2017-10-30 19:40:03
评测时间
2017-10-30 19:44:58
评测机
分数
100
总耗时
3126ms
峰值内存
16.492 MiB