0

|

1

/   \

2      3

//parent为并查集，FIND为并查集的查找操作//QUERY为询问结点对集合//TREE为基图有根树 Tarjan(u)      visit[u] = true      for each (u, v) in QUERY          if visit[v]              ans(u, v) = FIND(v)      for each (u, v) in TREE              if !visit[v]              Tarjan(v)              parent[v] = u

hdu2586 How far away ？

//============================================================================// Name        : hdu2586.cpp// Author      : birdfly// Description : 最近公共祖先//============================================================================#include <iostream>#include <string.h>#include <stdio.h>#define NN 40002 // number of house#define MM 202   // number of queryusing namespace std;typedef struct node{    int v;    int d;    struct node *nxt;}NODE;NODE *Link1[NN];NODE edg1[NN * 2]; // 树中的边NODE *Link2[NN];NODE edg2[NN * 2]; // 询问的点对int idx1, idx2, N, M;int res[MM][3]; // 记录结果，res[i][0]: u   res[i][1]: v  res[i][2]: lca(u, v)int fat[NN];int vis[NN];int dis[NN];void Add(int u, int v, int d, NODE edg[], NODE *Link[], int &idx){    edg[idx].v = v;    edg[idx].d = d;    edg[idx].nxt = Link[u];    Link[u] = edg + idx++;    edg[idx].v = u;    edg[idx].d = d;    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]){            res[p->d][2] = find(p->v); // 存的是最近公共祖先结点        }    }    for (NODE *p = Link1[u]; p; p = p->nxt){        if(!vis[p->v]){            dis[p->v] = dis[u] + p->d;            Tarjan(p->v);            fat[p->v] = u;        }    }}int main() {    int T, i, u, v, d;    scanf("%d", &T);    while(T--){        scanf("%d%d", &N, &M);        idx1 = 0;        memset(Link1, 0, sizeof(Link1));        for (i = 1; i < N; i++){            scanf("%d%d%d", &u, &v, &d);            Add(u, v, d, edg1, Link1, idx1);        }        idx2 = 0;        memset(Link2, 0, sizeof(Link2));        for (i = 1; i <= M; i++){            scanf("%d%d", &u, &v);            Add(u, v, i, edg2, Link2, idx2);            res[i][0] = u;            res[i][1] = v;        }        memset(vis, 0, sizeof(vis));        dis[1] = 0;        Tarjan(1);        for (i = 1; i <= M; i++){            printf("%d\n", dis[res[i][0]] + dis[res[i][1]] - 2 * dis[res[i][2]]);        }    }    return 0;}

poj1470  Closest Common Ancestors

//============================================================================// Name        : poj1470.cpp// Author      : birdfly// Description : 最近公共祖先//============================================================================#include <iostream>#include <stdio.h>#include <string.h>#define NN 902using 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;}

posted on 2010-11-03 11:57 ylfdrib 阅读(...) 评论(...) 编辑 收藏