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的更多相关文章

  1. 【POJ 1470】 Closest Common Ancestors

    [题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...

  2. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  3. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  4. poj1470 Closest Common Ancestors [ 离线LCA tarjan ]

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

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

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

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

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

  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(LCA)

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

  9. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

随机推荐

  1. careercup-栈与队列 3.3

    3.3 栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下.因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起.实现数据结构SetOfStacks 来模拟这种情况.Se ...

  2. xcode 4 安装cocos2d-x 2.1.4

    http://blog.csdn.net/xiaominghimi/article/details/6937685 从今天开始Himi将陆续更新cocos2d-X的博文,毕竟cocos2d-X的跨平台 ...

  3. Git学习笔记(一)

    刚开始学Git,记录下来,讹误之处还望指教. 安装好git之后,有两个东东,如图: git bash 表示是命令行操作. git GUI 表示是图形化操作.但是这个界面用起来不方便,另一个图形化操作工 ...

  4. XC应用系列作品(Android应用)

    XC系列应用,如真题园手机客户端1.1等应用已经分别在 360手机助手.腾讯应用宝.百度手机助手.小米应用商店.豌豆荚.应用汇.木蚂蚁等安卓市场平台上线了! 本页面的系列应用是本人的开发的一Andro ...

  5. GUI编程笔记(java)06:GUI窗体添加按钮并对按钮添加事件案例

    1.需求:把按钮添加到窗体,并对按钮添加一个点击事件. 步骤: (1)创建窗体对象(2)创建按钮对象(3)把按钮添加到窗体(4)窗体显示 2.编写程序思路: 窗体布局:窗体中组件的排列方式  布局分类 ...

  6. vs2012 aspx 没有设计视图了?

    vs2012的html设计视图没有了!重新安装一次都不行!现在已经通过简单办法来解决了 其实当你打开 HTML设计器 设置时, “启用 HTML设计器"  这里是打勾的!这时千万不要放弃.先 ...

  7. CentOS7上GitHub/GitLab多帐号管理SSH Key

    由于公司团队使用 GitLab 来托管代码,同时,个人在 Github 上还有一些代码仓库,可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不同的,这就造成了冲突 ,文章提供此类问题的解 ...

  8. DOS和UNIX文本文件之间相互转换的方法

    在Unix/Linux下可以使用file命令查看文件类型,如下: file dosfile.txt 使用dos2unix 一般Linux发行版中都带有这个小工具,只能把DOS转换为UNIX文件,命令如 ...

  9. table完美css样式,table的基本样式,table样式

    table完美css样式,table的基本样式,table样式 >>>>>>>>>>>>>>>>> ...

  10. [视频转换] C#VideoConvert视频转换帮助类 (转载)

    点击下载 VideoConvert.zip 主要功能如下 .获取文件的名字 .获取文件扩展名 .获取文件类型 .视频格式转为Flv .生成Flv视频的缩略图 .转换文件并保存在指定文件夹下 .转换文件 ...