【POJ1470】Closest Common Ancestors
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
【题意】
求LCA。
【分析】
跟上一题差不多,注意输入,没有那么复杂的。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 10010
#define INF 100000000 int fa[Maxn],first[Maxn],size[Maxn],dep[Maxn],son[Maxn];
int w[Maxn],top[Maxn];int wl;
bool q[Maxn];
int sum[Maxn]; struct node
{
int x,y,next;
}t[*Maxn];int len; int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} void dfs1(int x,int f)
{
fa[x]=f;dep[x]=dep[f]+;size[x]=;
son[x]=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
dfs1(t[i].y,x);
size[x]+=size[t[i].y];
if(size[t[i].y]>size[son[x]]) son[x]=t[i].y;
}
} void dfs2(int x,int tp)
{
w[x]=++wl;
top[x]=tp;
if(size[x]!=) dfs2(son[x],tp);
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa[x]&&t[i].y!=son[x])
{
dfs2(t[i].y,t[i].y);
}
} int LCA(int a, int b)
{
while ()
{
if(top[a]==top[b]) return dep[a]<=dep[b]?a:b;
else if(dep[top[a]]>=dep[top[b]]) a=fa[top[a]];
else b=fa[top[b]];
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(first,,sizeof(first));
memset(q,,sizeof(q));
len=;
for(int i=;i<=n;i++)
{
int x,y,z;
scanf("%d:(%d) ",&x,&y);
while(y--)
{
scanf("%d",&z);
ins(x,z);q[z]=;
}
}
int root;
for(int i=;i<=n;i++) if(!q[i]) root=i;
dep[]=;size[]=;
dfs1(root,);wl=;
dfs2(root,root);
int m;
scanf("%d",&m);getchar();
memset(sum,,sizeof(sum));
for(int i=;i<=m;i++)
{
int x,y;
scanf(" (%d %d)",&x,&y);
sum[LCA(x,y)]++;
}
for(int i=;i<=n;i++) if(sum[i]!=)
printf("%d:%d\n",i,sum[i]);
}
return ;
}
[POJ1470]
2016-05-10 13:14:31
【POJ1470】Closest Common Ancestors的更多相关文章
- 【POJ 1470】 Closest Common Ancestors
[题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
随机推荐
- careercup-栈与队列 3.3
3.3 栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下.因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起.实现数据结构SetOfStacks 来模拟这种情况.Se ...
- JDBC Transaction Management Example---reference
In this post, we want to talk about JDBC Transactions and how we can manage the operations in a data ...
- mongnodb 启动脚本
开始用mongodb建立一套监控体系,安装解压即可.附上编写的mongodb启动管理脚本. 建议 mkdir sbin 目录,放到sbin目录下.废话少说,代码如下: #!/bin/bash MONG ...
- 网络学习笔记----01--pathping跟踪数据包路径
操作系统win7 Pathping主要用于提供有关在来源和目标之间的中间跃点处的网络滞后和网络丢失的信息. Pathping将多个回显请求消息发送到来源和目标之间的各个路由器一段时间,然后根据各个路由 ...
- springmvc 返回xml
需求: 1.springmvc返回xml: 技术及环境: Spring 4.3.1.RELEASE JDK 1.8 IDEA 15.0.6 Maven 3 实现: spirngxml的配置主要如下: ...
- Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]
1. FileInputStream读取数据一次一个字节数组byte[ ] 使用FileInputStream一次读取一个字节数组: int read(byte[] b) 返回值:返回值其实是实际 ...
- iOS UIKit:TableView之表格创建(1)
Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...
- eclipse同时开两个tomcat
首先设置环境变量: 接着修改其中一个tomcat下bin文件夹的startup.bat和catalina.bat 将里面所有CATALINA_HOME都修改为CATALINA_HOME2 然后 修改c ...
- webapi 接口规则
[HttpPost] [AuthorizeFilter] public HttpResponseMessage DeleteStudentInfo([FromBody] object value) { ...
- php快速定位多维数组的深度
原文地址:php快速定位多维数组的深度作者:陌上花开 自定义一个函数: function array_depth($array) { $max_depth = 1; foreach ($array ...