任意门: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: 

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

POJ 1470 Closest Common Ancestors 【LCA】的更多相关文章

  1. POJ 1470 Closest Common Ancestors【LCA Tarjan】

    题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...

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

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  3. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  4. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

  5. POJ 1330:Nearest Common Ancestors【lca】

    题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么 思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上 #include<cstdio&g ...

  6. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  7. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  8. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  9. poj 1470 Closest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...

随机推荐

  1. linux_api之进程环境(二)

      本篇索引: 1.引言 2.终端登录 3.进程组 4.会话期 1.引言 通过上一篇的学习,我们已经知道了如何控制一个进程,fork函数从父进程中复制出子进程,我们可以通过exec函数让子进程运行新的 ...

  2. 关于GBK、GB2312、UTF8之间的区别

    UTF-8:Unicode Transformation Format-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为( ...

  3. (二)HTML中的部分标签

    HTML作为一种超文本标记语言,其中用到了大量的标签,昨天主要看了HTML中的图像标签和表格标签. (一)图像标签 <img> <img src="url"/&g ...

  4. C#+ObjectArx CAD二次开发(2)

    前面开了一个头,这里添加几个功能的实现, //添加图层 private void LoadLayer() { Document acDoc = Application.DocumentManager. ...

  5. PAT 1025 PAT Ranking

    #include <cstdio> #include <cstdlib> #include <vector> #include <cstring> #i ...

  6. Java中Date()类 日期转字符串、字符串转日期的问题(已解决)

    Java中Date()类 日期转字符串.字符串转日期的问题 今天在写东西的时候突然发现一个问题,就是先new 一个Date()然后将生成的值转为字符串, 然后再将转换后的字符串再次用new Date( ...

  7. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

  8. jQuery可调整表和列宽插件-colResizable

    最基本的例子 引入JS <script src="js/jquery-1.8.0.min.js" type="text/javascript">&l ...

  9. 用户选择wordpress程序建站需要知道的一些事情 - 安全、优化速度、配置

    WordPress是我们使用最多的CMS程序之一,无论是我们的个人博客,还是企业网站,甚至中小型站点,我们都可以用WP程序部署.我们看到海外网站的时候,大部分都是用的WORDPRESS程序.在我们国人 ...

  10. 转:如何利用已有的切片文件生成TPK

    Tpk是ArcGIS 10.1即将推出的一种新的数据文件类型,主要是用于将切片文件打包形成离线地图包.Tpk可以在ArcGIS Runtime中作为切片底图被加载.在ArcGIS 10.1中Tpk的生 ...