POJ - 1330 Nearest Common Ancestors(倍增LCA)

裸的求LCA,用的倍增,就是为了记个模板,似懂非懂,以后有时间就弄懂这些算法(大概率不会了。。。。。。)

#include 
#include 
#include 

using namespace std;
const int MAXN = 10010;
const int DEG = 20;

struct Edge
{
    int to,Next;
}edge[MAXN * 2];
int head[MAXN],tot;
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].Next = head[u];
    head[u] = tot++;
}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
int fa[MAXN][DEG]; //表示结点i的第2^j个祖先
int deg[MAXN];//深度数组

void BFS(int root)
{
    queue que;
    deg[root] = 0;
    fa[root][0] = root;
    que.push(root);
    while(!que.empty()) {
        int tmp = que.front();
        que.pop();
        //倍增核心代码
        for(int i = 1; i < DEG; i++)
            fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1];
        for(int i = head[tmp]; i != -1; i = edge[i].Next) {
            int v = edge[i].to;
            if(v == fa[tmp][0]) continue;
            deg[v] = deg[tmp] + 1;
            fa[v][0] = tmp;
            que.push(v);
        }
    }
}
int LCA(int u,int v)
{
    //让v总是最深的
    if(deg[u] > deg[v])
        swap(u,v);
    //调整到同样的深度
    int hu = deg[u],hv = deg[v];
    int tu = u,tv = v;
    for(int det = hv - hu, i = 0; det; det >>= 1, i++) {
        if(det & 1)
            tv = fa[tv][i];
    }
    if(tu == tv) return tu;
    //类似二分
    for(int i = DEG - 1; i >= 0; i--) {
        if(fa[tu][i] == fa[tv][i])
            continue;
        tu = fa[tu][i];
        tv = fa[tv][i];
    }
    return fa[tu][0];
}
bool flag[MAXN];
int main(void)
{
    int T;
    int n;
    int u,v;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        init();
        memset(flag,false,sizeof(flag));
        for(int i = 1; i < n; i++) {
            scanf("%d %d",&u,&v);
            addedge(u,v);
            addedge(v,u);
            flag[v] = true;
        }
        int root;
        for(int i = 1; i <= n; i++) {
            if(!flag[i]) {
                root = i;
                break;
            }
        }
        BFS(root);
        scanf("%d %d",&u,&v);
        printf("%d\n",LCA(u,v));
    }
    return 0;
}

 

你可能感兴趣的:(ACM-图论)