POJ——T 1470 Closest Common Ancestors
http://poj.org/problem?id=1470
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 20830 | Accepted: 6617 |
Description
Input
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 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
Source
#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的更多相关文章
- 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】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 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, 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: 20804 Accept ...
- 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 ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
随机推荐
- lsof 命令简介
losf 命令可以列出某个进程打开的所有文件信息.打开的文件可能是普通的文件,目录,NFS文件,块文件,字符文件,共享库,常规管道,明明管道,符号链接,Socket流,网络Socket,UNIX域So ...
- 关于JavaScript中this的指向,你知晓几分?请速来围观!
---恢复内容开始--- 一.this是什么东东? this是指包含它的函数作为方法被调用时所属的对象.这句话理解起来跟卵一样看不懂,但是如果你把它拆分开来变成这三句话后就好理解一点了. 1.包含它的 ...
- php>$_SERVER服务的一些常用命令
$_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVER['REMOTE_HOST'] //当前用户主机名 $_SERVER['REQUEST_URI'] //UR ...
- [codevs3955]最长严格上升子序列(加强版)
题目大意:给你一个序列,要你求该序列中最长严格上升子序列的长度. 解题思路:此题算是一道LIS模板题.普通的$O(n^2)$的LIS是会TLE的,因为$n\le 1000000$,所以此题要用单调队列 ...
- spring中IOC的简单使用
spring的ioc主要就是依赖注入,有基于构造器的依赖注入还有通过设值注入,这里我只简单的实现设值注入的方法,通过spring的依赖管理,我们可以很方便的了解各层之间的依赖关系,降低了各层之间的耦合 ...
- python如何命令行下载包
$ wget https://bootstrap.pypa.io/get-pip.py $ python get-pip.py $ pip -V #查看pip版本 $ pip install ...
- max带来的冲突
题目要求: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名:sum123.cpp * 作 者:林海云 * 完毕日期:20 ...
- c++_benchMark_vector_list_deque
title: c++_benchMark_vector_list_deque date: 2015-08-01 22:32:39 作者:titer1 + ZhangYu 出处:www.drysalte ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...
- 检测浏览器是否支持range
昨天的滑块建立在Input range这个基础上 这是IOS5.0及以后才支持的,而且在android2.3以下表现也不对 昨天的检测方式 var input = document.createEl ...