关闭

poj 1470 | zoj 1141 Closest Common Ancestors

标签: struct算法
196人阅读 评论(0) 收藏 举报
分类:

类型:LCA

题目:http://poj.org/problem?id=1470

来源:Southeastern Europe 2000

思路:使用LCA算法求最近公共祖先,并记录个数

// poj 1470 Closest Common Ancestors
// OLE AC 2956K	563MS
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int MAXN = 1100;
const int MAXM = 210000;

bool vis[MAXN];
int cnt, cntt, n;
int head[MAXN], p[MAXN], ru[MAXN], ancestor[MAXN], endd[MAXN];
vector<int> q[MAXN];
struct edge {
    int v, nxt;
}e[MAXM];

void addedge(int u, int v) {
    e[cnt].v = v;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void makeSet() {
	for(int i = 1; i <= n; ++i)
		p[i] = i;
}

int find(int u) {
	return (u != p[u]) ? (p[u] = find(p[u])) : p[u];
}

void link(int x, int y) {
    p[x] = y;
}

void LCA(int u) {
    int v, i;

    ancestor[u] = u;
    for(i = head[u]; i != -1; i = e[i].nxt) {
        v = e[i].v;
        LCA(v);
        link(find(v), find(u));
        ancestor[find(u)] = u;
    }
    vis[u] = true;
    int size = q[u].size();
    FOR(i, 0, size)
        if(vis[q[u][i]])
            ++endd[ancestor[find(q[u][i])]];
}

void init() {
    int i;

    CLR(head, -1);
    CLR(p, 0);
    CLR(endd, 0);
    CLR(ru, 0);
    CLR(ancestor, 0);
    CLR(vis, false);
    cnt = cntt = 0;
    //!!!
    makeSet();
    FORE(i, 1, n)
        q[i].clear();
}

int main() {
    int i, j, u, v, num;

    while(scanf("%d", &n) != EOF) {
        init();
        FORE(i, 1, n) {
            scanf("%d:(%d)", &u, &num);
            FOR(j, 0, num) {
                scanf("%d", &v);
                addedge(u, v);
                ++ru[v];
            }
        }
        scanf("%d", &num);
        FORE(j, 1, num) {
            scanf(" (%d %d)", &u, &v);
            q[u].push_back(v);
            q[v].push_back(u);
        }
        FORE(i, 1, n) if(ru[i] == 0) {
            LCA(i);
            break;
        }
        FORE(i, 1, n) if(endd[i] != 0)
            printf("%d:%d\n", i, endd[i]);
    }
    return 0;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:52950次
    • 积分:1737
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论