zoj 3195 Design the city

LCA

题意:给一个无根树,有q个询问,每个询问3个点,问将这3个点连起来,距离最短是多少,LCA的模板题,分别求LCA(X,Y),LCA(X,Z),LCA(Y,Z),和对应的距离,然后3个距离相加再除以2就是这个询问的结果

对于一对点,x,y, lca = LCA(x,y) , 那么点x到点y的距离为  dir[x] + dir[y] - 2 * dir[lca]  ;    其中dir[u]  表示点u到树根的距离

由于是模板题,只给代码,详细的讲解可以在学习笔记里面找《LCA与RMQ》

 

在线算法:LCA转RMQ

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int N = 50010;
const int M = 25;

int _pow[M];
int n,tot;
bool vis[N];
int ver[2*N],R[2*N],first[N],dir[N];
int dp[2*N][M];  //这个数组记得开到2*N,因为遍历后序列长度为2*n-1
struct node
{
    int v,w;
    node(int a, int b)
    { v = a; w = b;}
};
vector<node>g[N];

void dfs(int u ,int dep)
{
    vis[u] = true; ver[++tot] = u; first[u] = tot; R[tot] = dep;
    for(int i=0; i<g[u].size(); i++)
        if( !vis[g[u][i].v] )
        {
            int v = g[u][i].v , w = g[u][i].w;
            dir[v] = dir[u] + w;
            dfs(v,dep+1);
            ver[++tot] = u; R[tot] = dep;
        }
}

void ST(int len)
{
    int K = (int)(log((double)len) / log(2.0));
    for(int i=1; i<=len; i++) dp[i][0] = i;
    for(int j=1; j<=K; j++)
        for(int i=1; i+_pow[j]-1<=len; i++)
        {
            int a = dp[i][j-1] , b = dp[i+_pow[j-1]][j-1];
            if(R[a] < R[b]) dp[i][j] = a;
            else            dp[i][j] = b;
        }
}

int RMQ(int x ,int y)
{
    int K = (int)(log((double)(y-x+1)) / log(2.0));
    int a = dp[x][K] , b = dp[y-_pow[K]+1][K];
    if(R[a] < R[b]) return a;
    else            return b;
}

int LCA(int u ,int v)
{
    int x = first[u] , y = first[v];
    if(x > y) swap(x,y);
    int res = RMQ(x,y);
    return ver[res];
}

int main()
{
    for(int i=0; i<M; i++) _pow[i] = (1<<i);
    int cas = 0;
    while(scanf("%d",&n)!=EOF)
    {
        if(cas++) printf("\n");
        for(int i=0; i<n; i++) 
            g[i].clear() , vis[i] = false;
        for(int i=1; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].push_back(node(v,w));
            g[v].push_back(node(u,w));
        }
        tot = 0; dir[0] = 0;
        dfs(0,1);
        /*
        printf("节点 "); for(int i=1; i<=2*n-1; i++) printf("%d ",ver[i]); cout << endl;
        printf("深度 "); for(int i=1; i<=2*n-1; i++) printf("%d ",R[i]);   cout << endl;
        printf("首位 "); for(int i=0; i<n; i++) printf("%d ",first[i]);    cout << endl;
        printf("距离 "); for(int i=0; i<n; i++) printf("%d ",dir[i]);      cout << endl;
        */

        ST(2*n-1);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            int lca1 = LCA(x,y);
            int res1 = dir[x] + dir[y] - 2*dir[lca1];

            int lca2 = LCA(x,z);
            int res2 = dir[x] + dir[z] - 2*dir[lca2];

            int lca3 = LCA(y,z);
            int res3 = dir[y] + dir[z] - 2*dir[lca3];

            printf("%d\n",(res1 + res2 + res3)/2);
        }
    }
    return 0;
}

 

 

离线算法:Tarjan

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 50010;
const int M = 420010;

int n,tot;
int dir[N];
int fa[N];  //并查集
int ance[N]; //并查集的祖先
struct node
{
    int v,w;
    node(int a, int b)
    { v=a; w=b; }
};
vector<node>g[N];
bool vis[N];
int head[N];
struct ask
{
    int u,v,lca,c,next;
}ea[M];

int find(int x)
{//并查集查找元素且路径压缩
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void unionset(int x ,int y)
{//合并元素x和元素y所在的集合
    fa[find(y)] = find(x);
}

void Tarjan(int u)
{
    vis[u] = true;
    fa[u] = u;         //以点u建立集合,u为代表元素
    //ance[find(u)] = u; //该集合的祖先也是u自己
    ance[u] = u;
    for(int i=0; i<g[u].size(); i++)
        if( !vis[g[u][i].v])
        {
            int v = g[u][i].v , w = g[u][i].w;
            dir[v] = dir[u] + w;
            Tarjan(v);
            unionset(u,v); //将儿子所在的集合并到自己的集合里
            //ance[find(u)] = u; //保证自己所在的那个集合的祖先还是自己
        }

    for(int k=head[u]; k!=-1; k=ea[k].next)
        if( vis[ea[k].v] )
        {
            int v = ea[k].v;
            ea[k^1].lca = ea[k].lca = ance[find(v)];
        }
}

inline void add_ask(int u , int v ,int c)
{
    ea[tot].u = u; ea[tot].v = v; ea[tot].c = c; ea[tot].lca = -1;
    ea[tot].next = head[u]; head[u] = tot++;
    u = u^v; v = u^v; u = u^v;
    ea[tot].u = u; ea[tot].v = v; ea[tot].c = c; ea[tot].lca = -1;
    ea[tot].next = head[u]; head[u] = tot++;
}

int main()
{
    int cas = 0;
    while(scanf("%d",&n)!=EOF)
    {
        if(cas++) puts("");
        for(int i=0; i<n; i++)
            g[i].clear() , vis[i] = false;
        for(int i=1; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            g[u].push_back(node(v,w));
            g[v].push_back(node(u,w));
        }
        int q;
        tot = 0;
        memset(head,-1,sizeof(head));
        scanf("%d",&q);
        for(int i=0; i<q; i++)
        {
            int x,y,z;
            //要处理的询问包括  LCA(x,y),LCA(x,z),LCA(y,z)
            scanf("%d%d%d",&x,&y,&z);
            add_ask(x,y,i);
            add_ask(x,z,i);
            add_ask(y,z,i);
        }

        dir[0] = 0;
        Tarjan(0);

        for(int i=0; i<q; i++)
        {
            int s = i*6;
            int x = ea[s].u , y = ea[s].v , z = ea[s+2].v;
            int lca1 = ea[s].lca;     //LCA(x,y)
            int lca2 = ea[s+2].lca;   //LCA(x,z)
            int lca3 = ea[s+4].lca;   //LCA(y,z)
            int res1 = dir[x] + dir[y] - 2*dir[lca1];
            int res2 = dir[x] + dir[z] - 2*dir[lca2];
            int res3 = dir[y] + dir[z] - 2*dir[lca3];
            printf("%d\n",(res1+res2+res3)/2);
        }
    }
    return 0;
}

 

posted @ 2013-05-26 22:10  Titanium  阅读(1108)  评论(1编辑  收藏  举报