###### poj 1470 | zoj 1141 Closest Common Ancestors

// 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;
}

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);
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(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);
++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;
}



#### Zoj 1141 Closest Common Ancestors

2012-09-11 19:33:52

#### poj 1470 Closest Common Ancestors

2014-09-27 12:05:01

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

2013-10-23 15:03:20

#### poj 1470 Closest Common Ancestors

2012-10-09 00:29:14

#### POJ 1470 Closest Common Ancestors

2016-04-02 12:20:46

#### POJ 1470 — Closest Common Ancestors

2015-04-21 19:44:28

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

2013-10-05 00:15:06

#### poj - 1470 - Closest Common Ancestors

2013-09-28 20:08:31

#### POJ 1470 Closest Common Ancestors

2013-09-26 18:12:33

#### poj 1470 Closest Common Ancestors

2013-07-12 01:03:15

## 不良信息举报

poj 1470 | zoj 1141 Closest Common Ancestors