现在的位置: 首页 > 综合 > 正文

poj 1470 Closest Common Ancestors 离线算法Tarjan求LCA

2013年08月18日 ⁄ 综合 ⁄ 共 2698字 ⁄ 字号 评论关闭

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form: 

nr_of_vertices 
vertex:(nr_of_successors) successor1 successor2 ... successorn 
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ... 

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times 
For example, for the following tree: 

Sample Input

5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
      (2 3)
(1 3) (4 3)

Sample Output

2:1
5:5

Hint

Huge input, scanf is recommended.
#include <iostream>
#include <stdio.h>
#include <string.h>
#define NN 902
using namespace std;

typedef struct node{
    int v;
    struct node *nxt;
}NODE;

NODE edg1[NN * 2], edg2[NN * 1000];//数组要开大点
NODE *Link1[NN], *Link2[NN];

int idx1, idx2, N, M;
int fat[NN];
int vis[NN];
int cnt[NN];

void Init(NODE *Link[], int &idx){
    memset(Link, 0, sizeof(Link[0]) * (N + 1));
    idx = 0;
}

void Add(int u, int v, NODE edg[], NODE *Link[], int & idx){
    edg[idx].v = v;
    edg[idx].nxt = Link[u];
    Link[u] = edg + idx++;
    edg[idx].v = u;
    edg[idx].nxt = Link[v];
    Link[v] = edg + idx++;
}

int find(int x){
    if(x != fat[x]){
        return fat[x] = find(fat[x]);
    }
    return x;
}

void Tarjan(int u){
    vis[u] = 1;
    fat[u] = u;
    for (NODE *p = Link2[u]; p; p = p->nxt){
        if(vis[p->v]){
            cnt[find(p->v)]++;
        }
    }

    for (NODE *p = Link1[u]; p; p = p->nxt){
        if(!vis[p->v]){
            Tarjan(p->v);
            fat[p->v] = u;
        }
    }
}
int main() {
    int i, u, v, n, root;
    int flag[NN];
    while(scanf("%d", &N) != EOF){

        Init(Link1, idx1);
        memset(flag, 0, sizeof(flag));
        for (i = 1; i <= N; i++){ //数据的读入方式很不错啊
            scanf("%d", &u);
            while(getchar() != '(');
            scanf("%d", &n);
            while(getchar() != ')');
            while(n--){
                scanf("%d", &v);
                flag[v] = 1;
                Add(u, v, edg1, Link1, idx1);
            }
        }
        scanf("%d", &M);
        Init(Link2, idx2);
        for (i = 1; i <= M; i++){
            while(getchar() != '(');
            scanf("%d%d", &u, &v);
            while(getchar() != ')');
            Add(u, v, edg2, Link2, idx2);
        }

        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));

        for (i = 1; i <= N; i++){// 第一个结点不一定是根结点
            if(flag[i] == 0) break;
        }
        root = i;
        Tarjan(root);

        for (i = 1; i <= N; i++){
            if(cnt[i]){
                printf("%d:%d\n", i, cnt[i]);
            }
        }
    }
    return 0;
}

抱歉!评论已关闭.