Closest Common Ancestors POJ 1470
|
Language:
Default 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 The input file contents several data sets (at least one). 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 Sample Output 2:1 Hint Huge input, scanf is recommended.
题目大意:统计某个值做公共最近祖先的次数
//主要就是在最后输入的地方要做一下处理
|
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
const int N=1E4+;
typedef long long ll;
ll bits[];
bool pre[N];
int fa[N][],depth[N];
vector<int >ve[N];
map<int ,int >mp;
map<int ,int >::iterator it;
void inint(){
bits[]=;
for(int i=;i<;i++){
bits[i]=bits[i-]<<;
}
}
void dfs(int x,int y){
depth[x]=depth[y]+;
fa[x][]=y;
for(int i=;i<;i++) fa[x][i]=fa[fa[x][i-]][i-]; for(int i=;i<ve[x].size();i++){
int dx=ve[x][i];
if(dx!=y){
dfs(dx,x);
}
}
}
int lca(int x,int y){
if(depth[x]<depth[y]) swap(x,y);
int dif=depth[x]-depth[y];
for(int i=;i>=;i--){
if(dif>=bits[i]){
x=fa[x][i];
dif-=bits[i];
}
}
if(x==y) return x;
for(int i=;i>=;i--){
if(depth[x]>=bits[i]&&fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
int main(){
int t;
inint();
while(~scanf("%d",&t)){
memset(pre,,sizeof(pre));
memset(depth,,sizeof(depth));
memset(fa,,sizeof(fa));
for(int i=;i<=t;i++){
ve[i].clear();
}
for(int i=;i<=t;i++){
int x,y;
scanf("%d:(%d)",&x,&y);
while(y--){
int x1;
scanf("%d",&x1);
pre[x1]=;
ve[x].push_back(x1);
ve[x1].push_back(x);
}
}
int ancestor;
for(int i=;i<=t;i++){
if(pre[i]==){
ancestor=i;
break; }
}
dfs(ancestor,);
int n;
scanf("%d",&n); for(int i=;i<=n;i++){
int x,y;
char xx;
while(xx=getchar()){
if(xx=='(')
break;
}
scanf("%d%d)",&x,&y);
mp[lca(x,y)]++;
}
for(it=mp.begin();it!=mp.end();it++){
if(it->second!=){
printf("%d:%d\n",it->first,it->second);
}
}
mp.clear();
}
return ;
}
Closest Common Ancestors POJ 1470的更多相关文章
- 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,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- 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
传送门 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)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- BNUOJ 1589 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on PKU ...
随机推荐
- spring 请求静态资源文件
在springMVC项目中使用restful风格写,需要到web.xml配置全拦截. <servlet> <servlet-name>springmvc</ ...
- Java进阶之心态
不管什么时候学习都是一个积累的过程,量变才能引起质变.一口吃一个胖子是不存在的,成长的路上没有捷径,只有学到的知识才是我们走向远方道路的基石!
- bugku论剑场web解题记录
前言 国庆这几天感觉没什么好玩的地方,家又离的太远,弱鸡的我便决定刷刷题涨涨知识,于是就有了这篇文章.. 正文 写的不对的地方欢迎指正 web26 打开直接就是代码,这应该就是一道代码审计的题了 这里 ...
- 北邮OJ-257- 最近公共祖先-软件14 java
思路分析:思路应该比较简单也很容易想的来,就是比较两个节点的最近的祖先节点,要对每个节点依次记录下他的所有祖先节点,包括其自己,因为自己也算自己的祖先节点,这一点题目中没有明确指出 所以比较坑. 我们 ...
- 卷积的发展历程,原理和基于 TensorFlow 的实现
欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 稀疏交互 在生物学家休博尔和维瑟尔早期关于猫视觉皮层的研究中发现, ...
- coding++:高并发解决方案限流技术---漏桶算法限流--demo
1.漏桶算法 漏桶作为计量工具(The Leaky Bucket Algorithm as a Meter)时,可以用于流量整形(Traffic Shaping)和流量控制(TrafficPolici ...
- VS2015 远程调试:Remote Debugger
一.关于Remote Debugger 使用VS远程调试器Remote Debugger,我们可以调试部署在不同机器上的应用程序,如桌面应用程序和Asp.Net应用程序. 二.Remote Debug ...
- python——新excel模块之openpyxl
1.安装 pip install openpyxl 2.新建文件 book=openpyxl.Workbook() 3.打开sheet页(两种方式) sheet=book.active #默认的she ...
- centos7中安装redis
http://www.open-open.com/lib/view/open1426468117367.html https://www.cnblogs.com/cndavidwang/p/64294 ...
- [codevs]1250斐波那契数列<矩阵乘法&快速幂>
题目描述 Description 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30 ...
