poj 1470 | zoj 1141 Closest Common Ancestors

原创 2012年03月23日 18:39:51

类型: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;
}




相关文章推荐

poj 1470 || zoj 1141 Closest Common Ancestors (LCA)

题目链接:  poj 1470 题目大意:  给出一棵树,然后有有限次查询(a,b)                   每次查询节点a与节点b的最近公共祖先                   输出...

POJ1470/ZOJ1141 Closest Common Ancestors(LCA离线算法)

题意很明显?真不觉的,我去啊,我是看了人家的翻译才明白的,那家伙,输入格式相当销魂啊…… 大意是说,给出一棵树(包含 N 个点),然后给出 M 次询问,每次询问都是两个点的编号,他们一定有一个最近公...

TOJ 3033 ZOJ 1141 POJ 1470 Closest Common Ancestors / 最近公共祖先

Closest Common Ancestors 时间限制(普通/Java):2000MS/6000MS     运行内存限制:65536KByte 描述 Write a program th...

Zoj 1141 Closest Common Ancestors

题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=141 思路:LCA->RMQ + DFS + ST(在线算法) ...
  • niuox
  • niuox
  • 2012年09月11日 19:33
  • 618

POJ 1470 Closest Common Ancestors

LCA,输入很烦,但是冒号和括号可以直接过滤掉

POJ 1470 Closest Common Ancestors

一个裸的LCA,唯一的坑点就在于输入(╯‵□′)╯︵┻━┻,不得不GG #include #include #include #include #include #include usin...

poj1470 Closest Common Ancestors 离线LCA

Language: Default Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K ...

poj1470(Closest Common Ancestors)

Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions...
  • highacm
  • highacm
  • 2013年04月18日 20:47
  • 390

【LCA最近公共祖先】 poj1470 Closest Common Ancestors

It's a shame that I have wasted too much time on it... But it's just a simple LCA problem, and we ca...

POJ 1470 Closest Common Ancestors LCA Tarjan算法

这道题最终的计算结果和询问次序无关,绝对是练习lca的tarjan算法的不二之选~...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1470 | zoj 1141 Closest Common Ancestors
举报原因:
原因补充:

(最多只允许输入30个字)