http://poj.org/problem?id=1470

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 20830   Accepted: 6617

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.

Source

 
LCA ,,(多组数据、)
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(2e5+);
int n,m,cnt;
int ans[N]; int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):
v(v),next(next){}
}edge[N<<];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int son[N],size[N],deep[N],top[N],dad[N],fa[N];
void DFS(int u,int fa,int deepth)
{
size[u]=;
dad[u]=fa;
deep[u]=deepth;
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]==v) continue;
DFS(v,u,deepth+);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
void DFS_(int u,int Top)
{
top[u]=Top;
if(son[u]) DFS_(son[u],Top);
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]!=v&&son[u]!=v) DFS_(v,v);
}
}
int LCA(int x,int y)
{
for(;top[x]!=top[y];x=dad[top[x]])
if(deep[top[x]]<deep[top[y]]) swap(x,y);
return deep[x]<deep[y]?x:y;
} inline void init()
{
sumedge=;
memset(fa,,sizeof(fa));
memset(dad,,sizeof(dad));
memset(top,,sizeof(top));
memset(son,,sizeof(son));
memset(ans,,sizeof(ans));
memset(size,,sizeof(size));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
memset(deep,,sizeof(deep));
} inline void read(int &x)
{
x=;register char ch=getchar();
for(;ch<''||ch>'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int main()
{
for(int t;~scanf("%d",&t);init())
{
n=t;
for(int u,v,nn;t--;)
{
read(u);
read(nn);
for(int i=;i<=nn;i++)
{
read(v);
fa[v]=u;
ins(u,v);
ins(v,u);
}
}
int root=;
for(;root<=n;root++)
if(!fa[root]) break;
DFS(root,,);
DFS_(root,root);
read(m);
for(int u,v;m--;)
{
read(u),read(v);
ans[LCA(u,v)]++;
}
for(int i=;i<=n;i++)
if(ans[i]) printf("%d:%d\n",i,ans[i]);
}
return ;
}

POJ——T 1470 Closest Common Ancestors的更多相关文章

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

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

  2. POJ 1470 Closest Common Ancestors 【LCA】

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

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

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

  4. POJ 1470 Closest Common Ancestors

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

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

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

  6. poj——1470 Closest Common Ancestors

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

  7. 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 ...

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

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

  9. POJ 1470 Closest Common Ancestors【LCA Tarjan】

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

随机推荐

  1. 改变GridView中列的宽度

    <asp:TemplateField HeaderText="规格型号" HeaderStyle-Width="24%">

  2. Git 环境安装

    本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.14.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...

  3. 利用IP安全策略关闭危险端口

    默认情况下,Windows服务器有很多端口是开放的,网络病毒和黑客可以通过这些端口连上你的服务器并进行攻击. 为了让你的系统变为铜墙铁壁,应该封闭这些端口,主要有:TCP 135.139.445.59 ...

  4. Rx = Observables + LINQ + Schedulers

    The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...

  5. h5实现 微信的授权登录

    本文重点 判断是不是微信环境 localstorage设置一个值 微信授权登录 获取一个时间戳 new Date().getTime() const wx = (function () { retur ...

  6. bytes、str与unicode

    1.Python3字符序列的类型 bytes -> 原始的8位值(既字节) str -> Unicode字符 2.Python2字符序列的类型 str -> 原始的8位值(既字节) ...

  7. 死锁的Dump文件

    死锁的Dump文件 package com.stono.thread; public class DeadLockDemo { private static String A = "A&qu ...

  8. Cube Simulation zoj3429 模拟

    Description Here's a cube whose size of its 3 dimensions are all infinite. Meanwhile, there're 6 pro ...

  9. zzulioj--1746--三角形面积(几何水题)

    1746: 三角形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 100  Solved: 31 SubmitStatusWeb Board De ...

  10. Gcc/MinGW/Cygwin/Msys 分别是什么?

    一.GCC的历史 GCC是一个原本用于Unix-like系统下编程的编译器. 不过,现在GCC也有了许多Win32下的移植版本. 所以,也许对于许多Windows开发者来说,GCC还是一个比较陌生的东 ...