poj 1470 Closest Common Ancestors 离线算法Tarjan求LCA
2011-07-08 17:53
260 查看
DescriptionWrite 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)InputThe 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.OutputFor 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
#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;
}
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.OutputFor 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:5HintHuge 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;
}
相关文章推荐
- POJ 1470 Closest Common Ancestors(离线tarjan-LCA)
- POJ 1470 LCA tarjan 离线算法
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- poj1470 LCA Tarjan
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- LCA三种算法学习(离线算法tarjan+在线算法转rmq+在线倍增)例题poj1330、1470;hdu4547、2874
- poj 1470 Closest Common Ancestors (离线LCA Tarjan)
- POJ 1470 Closest Common Ancestors LCA_Tarjan
- LCA的tarjan求法&&POJ 1470的辛酸历程
- POJ1470 Closest Common Ancestors(最近公共祖先lca,离线Tarjan)
- poj 1470 Closest Common Ancestors tarjan求lca和树的孩子兄弟表示
- POJ-1470 Closest Common Ancestors Tarjan-LCA
- poj 1470--tarjan--LCA
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
- poj 1470 关于lca离线tarjan方法的使用
- poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】
- POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
- poj 1470 lca(tarjan做的)