POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 13372   Accepted: 4340

Description

Write 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)

Input

The 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.

Output

For 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: 
POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

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:5

Hint

Huge input, scanf is recommended.
 /* ***********************************************
Author :kuangbin
Created Time :2013-9-5 9:11:48
File Name :F:\2013ACM练习\专题学习\LCA\POJ1470_2.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/*
* POJ 1470
* 给出一颗有向树,Q个查询
* 输出查询结果中每个点出现次数
*/
/*
* LCA离线算法,Tarjan
* 复杂度O(n+Q);
*/
const int MAXN = ;
const int MAXQ = ;//查询数的最大值 //并查集部分
int F[MAXN];//需要初始化为-1
int find(int x)
{
if(F[x] == -)return x;
return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
F[t1] = t2;
}
//************************
bool vis[MAXN];//访问标记
int ancestor[MAXN];//祖先
struct Edge
{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Query
{
int q,next;
int index;//查询编号
}query[MAXQ*];
int answer[MAXQ];//存储最后的查询结果,下标0~Q-1
int h[MAXQ];
int tt;
int Q; void add_query(int u,int v,int index)
{
query[tt].q = v;
query[tt].next = h[u];
query[tt].index = index;
h[u] = tt++;
query[tt].q = u;
query[tt].next = h[v];
query[tt].index = index;
h[v] = tt++;
} void init()
{
tot = ;
memset(head,-,sizeof(head));
tt = ;
memset(h,-,sizeof(h));
memset(vis,false,sizeof(vis));
memset(F,-,sizeof(F));
memset(ancestor,,sizeof(ancestor));
} void LCA(int u)
{
ancestor[u] = u;
vis[u] = true;
for(int i = head[u];i != -;i = edge[i].next)
{
int v = edge[i].to;
if(vis[v])continue;
LCA(v);
bing(u,v);
ancestor[find(u)] = u;
}
for(int i = h[u];i != -;i = query[i].next)
{
int v = query[i].q;
if(vis[v])
{
answer[query[i].index] = ancestor[find(v)];
}
}
} bool flag[MAXN];
int Count_num[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int u,v,k;
while(scanf("%d",&n) == )
{
init();
memset(flag,false,sizeof(flag));
for(int i = ;i <= n;i++)
{
scanf("%d:(%d)",&u,&k);
while(k--)
{
scanf("%d",&v);
flag[v] = true;
addedge(u,v);
addedge(v,u);
}
}
scanf("%d",&Q);
for(int i = ;i < Q;i++)
{
char ch;
cin>>ch;
scanf("%d %d)",&u,&v);
add_query(u,v,i);
}
int root;
for(int i = ;i <= n;i++)
if(!flag[i])
{
root = i;
break;
}
LCA(root);
memset(Count_num,,sizeof(Count_num));
for(int i = ;i < Q;i++)
Count_num[answer[i]]++;
for(int i = ;i <= n;i++)
if(Count_num[i] > )
printf("%d:%d\n",i,Count_num[i]);
}
return ;
}
上一篇:Nginx二级域名及多Server反向代理配置


下一篇:LightSpeed 相关问题处理