首页 技术 正文
技术 2022年11月11日
0 收藏 999 点赞 3,909 浏览 2927 个字

任意门:http://poj.org/problem?id=1470

Closest Common Ancestors

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 22519   Accepted: 7137

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】

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 个结点 N-1 条边的树,和 M 次查询;

形式是:先给根结点 然后 给与这个根结点相连的 子节点。

查询的对儿给得有点放荡不羁,需要处理一下空格、回车、括号。。。

解题思路:

虽说表面上看是一道裸得 LCA 模板题(简单粗暴Tarjan)

但是细节还是要注意:

本题没有给 查询数 M 的范围(RE了两次)所以要投机取巧一下不使用记录每对查询的 LCA。

本题是多测试样例,注意初始化!!!

AC code:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e3+;
struct Edge{int v, w, nxt;}edge[MAXN<<];
struct Query
{
int v, id;
Query(){};
Query(int _v, int _id):v(_v), id(_id){};
};
vector<Query> q[MAXN]; int head[MAXN], cnt;
int fa[MAXN], ans[MAXN<<], no[MAXN];
bool vis[MAXN], in[MAXN];
int N, M; void init()
{
memset(head, -, sizeof(head));
memset(in, false, sizeof(in));
memset(vis, false, sizeof(vis));
//memset(ans, 0, sizeof(ans));
memset(no, , sizeof(no));
cnt = ;
for(int i = ; i <= N; i++)
fa[i] = i, q[i].clear();
} int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);} void AddEdge(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void Tarjan(int s, int f)
{
int root = s;
fa[s] = s;
for(int i = head[s]; i != -; i = edge[i].nxt)
{
int Eiv = edge[i].v;
if(Eiv == f) continue;
Tarjan(Eiv, s);
fa[getfa(Eiv)] = s;
}
vis[s] = true;
for(int i = ; i < q[s].size(); i++){
//if(vis[q[s][i].v]) ans[q[s][i].id] = getfa(q[s][i].v);
if(vis[q[s][i].v])
no[getfa(q[s][i].v)]++;
}
} int main()
{
while(~scanf("%d", &N)){
init();
//scanf("%d", &N);
for(int i = , u, v, k; i <= N; i++){
scanf("%d:(%d)", &u, &k);
for(int j = ; j <= k; j++){
scanf("%d", &v);
AddEdge(u, v);
in[v] = true;
}
}
int root = ;
for(int i = ; i <= N; i++) if(!in[i]){root = i;break;}
scanf("%d", &M);
int sum = , u, v;
while(sum <= M){
while(getchar()!='(');
scanf("%d%d", &u, &v);
while(getchar()!=')');
q[u].push_back(Query(v, sum));
q[v].push_back(Query(u, sum));
sum++;
}
Tarjan(root, -);
/*
for(int i = 1; i <= M; i++){
no[ans[i]]++;
}
*/ for(int i = ; i <= N; i++){
if(no[i]) printf("%d:%d\n", i, no[i]);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298