poj——1470 Closest Common Ancestors
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 20804 | Accepted: 6608 |
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<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 10100
using namespace std;
char ch;
vector<int>vec[N],que[N];
int t,s,n,m,x,y,num,qx[N],qy[N],fa[N],dad[N],ans[N],root,ans1[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int find(int x)
{
if(fa[x]==x) return x;
fa[x]=find(fa[x]);
return fa[x];
}
int tarjan(int x)
{
fa[x]=x;
;i<vec[x].size();i++)
if(vec[x][i]!=dad[x])
dad[vec[x][i]]=x,tarjan(vec[x][i]);
;i<que[x].size();i++)
if(dad[y=qx[que[x][i]]^qy[que[x][i]]^x])
ans1[que[x][i]]=find(y);
fa[x]=dad[x];
}
void begin()
{
;i<=n;i++)
vec[i].clear(),que[i].clear();
memset(fa,,sizeof(fa));
memset(ans,,sizeof(ans));
memset(dad,,sizeof(dad));
memset(ans1,,sizeof(ans1));
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
s=t;begin();
while(t--)
{
x=read();
n=read();
;i<=n;i++)
{
y=read();fa[y]=x;
vec[x].push_back(y);
vec[y].push_back(x);
}
}
;i<=s;i++)
if(!fa[i]) root=i;
memset(fa,,sizeof(fa));
memset(ans,,sizeof(ans));
m=read();
;i<=m;i++)
{
qx[i]=read(),qy[i]=read();
que[qx[i]].push_back(i);
que[qy[i]].push_back(i);
}
tarjan(root);
;i<=m;i++)
ans[ans1[i]]++;
;i<=s;i++)
if(ans[i]) printf("%d:%d\n",i,ans[i]);
}
;
}
tarjan暴空间、、、
O(≧口≦)O气死了,蒟蒻表示以后再也不用tarjan了!!!!!!!!!!!!
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 910
using namespace std;
vector<int>vec[N];
int n,m,s,x,y,dad[N],fa[N],top[N],deep[N],size[N],ans[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int lca(int x,int y)
{
for(;top[x]!=top[y];)
{
if(deep[top[x]]<deep[top[y]])
swap(x,y);
x=fa[x];
}
if(deep[x]>deep[y])
swap(x,y);
return x;
}
int dfs(int x)
{
size[x]=;
deep[x]=deep[fa[x]]+;
;i<vec[x].size();i++)
if(vec[x][i]!=fa[x])
{
fa[vec[x][i]]=x;
dfs(vec[x][i]);
size[x]+=size[vec[x][i]];
}
}
int dfs1(int x)
{
;
if(!top[x]) top[x]=x;
;i<vec[x].size();i++)
if(vec[x][i]!=fa[x]&&size[t]<size[vec[x][i]])
t=vec[x][i];
if(t) top[t]=top[x],dfs1(t);
;i<vec[x].size();i++)
if(vec[x][i]!=fa[x]&&vec[x][i]!=t)
dfs1(vec[x][i]);
}
int begin()
{
;i<=n;i++)
vec[i].clear();
memset(fa,,sizeof(fa));
memset(top,,sizeof(top));
memset(ans,,sizeof(ans));
memset(dad,,sizeof(dad));
memset(deep,,sizeof(deep));
memset(size,,sizeof(size));
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
s=n;begin();
while(n--)
{
x=read();m=read();
;i<=m;i++)
{
y=read();dad[y]=x;
vec[x].push_back(y);
vec[y].push_back(x);
}
}
;i<=s;i++)
if(!dad[i])
{dfs(i);dfs1(i);break;}
m=read();
;i<=m;i++)
{
x=read(),y=read();
ans[lca(x,y)]++;
}
;i<=s;i++)
if(ans[i]) printf("%d:%d\n",i,ans[i]);
}
;
}
poj——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 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(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
- 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. ...
随机推荐
- 学JAVA第二十三天,List类型和Set类型
数组,是我们最常用的,但是有时候,我们要用数组,但是又不知道数组的类的长度的时候, 我们java就有一个很好用的工具Collection,这都是java的爸爸的用心良苦,Collection中包含Li ...
- js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题
最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...
- 支持中英文和国旗的android国家代码/国际电话区号选择器
最近在做app登录的时候,因为需要支持国外手机号注册和登录,所以就涉及到国际电话区号的选择.在github上面找了一下,国家名称基本都是只有英文版本,而手动的去把中文一个个加上实在是一件费时费力的事情 ...
- 使用过Fetch之后,你还想使用AJAX吗
之前做数据交互的时候,请求数据一直使用ajax,看到网上有使用Fetch,所以也想拿来尝尝鲜 本次介绍只涉及fetch相关,传统的ajax基本上不涉及 当然你也要考虑兼容.浏览器支持情况. 一会这个只 ...
- Ghost Win10系统X64位和32位10041装机版下载
更多系统下载尽在系统妈:http://www.xitongma.com 特别说明: 1.C:盘分区须至少15GB(安装过程有大量的解压临时文件),安装完成后C:盘占用10GB左右! 2.安装之后如有硬 ...
- leetcode_919. Complete Binary Tree Inserter
https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...
- TensorFlow低阶API(一)—— 简介
简介 本文旨在知道您使用低级别TensorFlow API(TensorFlow Core)开始编程.您可以学习执行以下操作: 管理自己的TensorFlow程序(tf.Graph)和TensorFl ...
- CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建
Android开发使用控件入门--环境搭建 2014-12-24 09:57 14人阅读 评论(0) 收藏 编辑 删除 CAD控件.CAD三维控件,手机 ...
- E. Wrong Answer
E. Wrong Answer time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Java B
3.继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么? 答:父类: package test; public class FatherClass { public FatherClass( ...