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 ...
随机推荐
- 使用SparkSQL编写wordCount的词频统计
# 使用SparkSQL编写wordCount的词频统计 ## word.txt```hello hello scala sparkjava sql html java hellojack jack ...
- flume面试题
1 你是如何实现Flume数据传输的监控的使用第三方框架Ganglia实时监控Flume. 2 Flume的Source,Sink,Channel的作用?你们Source是什么类型?1.作用 (1)S ...
- Leetcode——回溯法常考算法整理
Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...
- 基于 HTML5 WebGL 的故宫人流量动态监控系统
前言 在当代社会,故宫已经成为一个具有多元意义的文化符号,在历史.艺术.文化等不同领域发挥着重要的作用,在国际上也成为能够代表中国文化甚至中国形象的国际符号.近几年故宫的观众接待量逐年递增,年接待量已 ...
- python之面向对象的关系
一.从空间角度研究类 类外面可以给对象封装属性 class A: address = '美丽富饶的沙河' def __init__(self, name): self.name = name def ...
- OpenCV-Python 光流 | 四十八
目标 在本章中, 我们将了解光流的概念及其使用Lucas-Kanade方法的估计. 我们将使用cv.calcOpticalFlowPyrLK()之类的函数来跟踪视频中的特征点. 我们将使用cv.cal ...
- 如何通过 JavaCSV 类库来优雅地(偷懒)读写 CSV 文件?
欢迎关注笔者的公众号: 小哈学Java, 专注于推送 Java 领域优质干货文章!! 个人博客: https://www.exception.site/essay/how-to-create-read ...
- ES6中async与await的使用方法
promise的使用方法 promise简介 是异步编程的一种解决方案.从语法上说,Promise 是一个对象,从它可以获取异步操作的消息.解决回调函数嵌套过多的情况 const promise =n ...
- Material Design 组件之 CollapsingToolbarLayout
CollapsingToolbarLayout 主要用于实现一个可折叠的标题栏,一般作为 AppBarLayout 的子 View 来使用,下面总结一下 CollapsingToolbarLayout ...
- 封装属于自己的Python包
将自己的程序打包为whl/tar.gz文件 有时候自己写了一个开发基本类,我们把这个类打包为whl或者tar.gz文件,这样的话同事也可以使用自己开发的基本类了 安装setuptools pip in ...
