Closest Common Ancestors
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 21011 | Accepted: 6648 |
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.
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:

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:5
Hint
Huge input, scanf is recommended.
题目大意:
给出一棵树(包含 N 个点),然后给出 M 次询问,每次询问都是两个点的编号,他们一定有一个最近公共祖先,
最后让你输出每个点被当做最近公共祖先的次数(是 0 次的话就不输出了)
很裸的 LCA离线 算法
#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 = 1010;
const int MAXQ = 500010;//查询数的最大值
//并查集部分
int F[MAXN];//需要初始化为-1
int find(int x)
{
if (F[x] == -1)
{
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 * 2];
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 * 2];
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 = 0;
memset(head, -1, sizeof(head));
tt = 0;
memset(h, -1, sizeof(h));
memset(vis, false, sizeof(vis));
memset(F, -1, sizeof(F));
memset(ancestor, 0, sizeof(ancestor));
}
void LCA(int u)
{
ancestor[u] = u;
vis[u] = true;
for (int i = head[u]; i != -1; 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 != -1; 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()
{
int n;
int u, v, k;
while (scanf("%d", &n) == 1)
{
init();
memset(flag, false, sizeof(flag));
for (int i = 1; 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 = 0; i < Q; i++)
{
char ch;
cin >> ch;
scanf("%d %d)", &u, &v);
add_query(u, v, i);
}
int root;
for (int i = 1; i <= n; i++)
if (flag[i] == 0)
{
root = i;
break;
}
LCA(root);
memset(Count_num, 0, sizeof(Count_num));
for (int i = 0; i < Q; i++)
{
Count_num[answer[i]]++;
}
for (int i = 1; i <= n; i++)
if (Count_num[i] > 0)
{
printf("%d:%d\n", i, Count_num[i]);
}
}
return 0;
}