码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #294 Div2 E(A and B and Lecture Rooms)

时间:2015-03-02 14:53:01      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:lca   数据结构      

Problem

给一棵树,含有N个节点,N?1条边。进行M次查询,每次给定两个节点x,y,问树上有多少个节点到x,y的距离相同。

Limits

TimeLimit(ms):2000

MemoryLimit(MB):256

N,M[1,105]

x,y[1,N]

Look up Original Problem From here

Solution

求出a,b两个节点的lca,再找到lca?>alca?>b上的某个节点v使得dis(v,a)=dis(v,b)。分v=lcavlca?>avlca?>b三种情况讨论,求出答案即可。

答案与不同子树含有的节点总数有关,需要预处理。

Complexity

TimeComplexity:((N+M)×log2N)

MemoryComplexity:O(N×log2N)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN 200100
#define N 100100
#define M 20
struct Edge{
    int from,to,next;
}edge[MAXN];
int head[N],num_edge;
void add_Edge(int from,int to){
    int t=++num_edge;
    edge[t].from=from;
    edge[t].to=to;
    edge[t].next=head[from];
    head[from]=t;
}
int n,m,dfs_num;
int anc[N][M],height[N],father[N],fatheredge[N],nodeval[N];
void dfs_tree(int now,int fa,int h){
    height[now]=h;
    int num1=++dfs_num;
    for(int i=head[now];i!=-1;i=edge[i].next){
        int to=edge[i].to;
        if(to==fa){
            father[now]=fa;
            fatheredge[now]=i;
            continue;
        }
        dfs_tree(to,now,h+1);
    }
    nodeval[now]=dfs_num-num1+1;
}
void LCM_init(){
    repin(i,1,n){
        for(int j=0;1<<j<n;j++){
            anc[i][j]=-1;
        }
    }
    repin(i,1,n){
        anc[i][0]=father[i];
    }
    for(int j=1;1<<j<n;j++){
        repin(i,1,n){
            if(anc[i][j-1]!=-1) anc[i][j]=anc[anc[i][j-1]][j-1];
        }
    }
}
int find_LCA(int x,int y){
    if(height[x]<height[y]) swap(x,y);
    int log;
    for(log=0;1<<log<n;log++);
    log--;
    depin(i,log,0){
        if(height[x]-(1<<i)>=height[y]) x=anc[x][i];
    }
    if(x==y) return x;
    depin(i,log,0){
        if(anc[x][i]!=-1 && anc[x][i]!=anc[y][i]){
            x=anc[x][i];
            y=anc[y][i];
        }
    }
    return father[x];
}
int find_ans(int& x,int limith){
    int log;
    for(log=0;1<<log<n;log++);
    log--;
    depin(i,log,0){
        if(height[x]-(1<<i)>limith) x=anc[x][i];
    }
    int x1=father[x];
    return nodeval[x1];
}
int main(){
    scanf("%d",&n);
    repin(i,0,n){
        head[i]=-1;
    }
    repin(i,1,n-1){
        int a,b;
        scanf("%d %d",&a,&b);
        add_Edge(a,b);
        add_Edge(b,a);
    }
    dfs_tree(1,0,0);
    LCM_init();
    scanf("%d",&m);
    while(m--){
        int x,y,ans=0;
        scanf("%d %d",&x,&y);
        int lca=find_LCA(x,y);
        int leftdis=height[x]-height[lca];
        int rightdis=height[y]-height[lca];
        if(x==y) ans=n;
        else if((leftdis+rightdis)%2) ans=0;
        else if(leftdis==rightdis){
            ans=n;
            find_ans(x,height[lca]+(leftdis-rightdis)/2);
            ans-=nodeval[x];
            find_ans(y,height[lca]+(rightdis-leftdis)/2);
            ans-=nodeval[y];
        }
        else if(leftdis>rightdis){
            ans=find_ans(x,height[lca]+(leftdis-rightdis)/2);
            ans-=nodeval[x];
        }
        else{
            ans=find_ans(y,height[lca]+(rightdis-leftdis)/2);
            ans-=nodeval[y];
        }
        printf("%d\n",ans);
    }
}

Codeforces Round #294 Div2 E(A and B and Lecture Rooms)

标签:lca   数据结构      

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/44018035

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!