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 ...
随机推荐
- 从sslyze看TLS证书的点点滴滴
纵观眼下,https已经深入大街小巷,成为网络生活中不可或缺的一部分了.提到了https,我们又不得不想到TLS(SSL),而提到了TLS,我们又不得不提到一个让人捉摸不透的东西:TLS证书. 关于证 ...
- leetcode签到 892. 三维形体的表面积
题目 三维形体的表面积 在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上. 请你返回最 ...
- Hive设置配置参数的方法,列举8个常用配置
Hive设置配置参数的方法 Hive提供三种可以改变环境变量的方法,分别是: (1).修改${HIVE_HOME}/conf/hive-site.xml配置文件: (2).命令行参数: (3).在已经 ...
- POJ 1062 昂贵的聘礼 最短路+超级源点
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...
- ysoserial-C3P0 分析
环境准备: pom: <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> < ...
- 给社团同学做的R语言爬虫分享
大家好,给大家做一个关于R语言爬虫的分享,很荣幸也有些惭愧,因为我是一个编程菜鸟,社团里有很多优秀的同学经验比我要丰富的多,这次分享是很初级的,适用于没有接触过爬虫且有一些编程基础的同学,内容主要有以 ...
- iOS 性能优化点
用 ARC 管理内存 在正确的地方使用 reuseIdentifier 尽量把 views 设置为完全不透明 如果你有透明的 Views 你应该设置它们的 opaque(不透明)属性为 YES.例如一 ...
- 使用kibana操作elasticsearch7.x 教程
由于elasticsearch7.x取消了type(类型的概念)对应数据库表的概念 添加一个索引 PUT 索引名 { "settings": { "number_of_s ...
- es6的箭头函数和es5的function函数区别
一.es6的箭头函数es6箭头函数内部没有this,使用时会上朔寻找最近的this不可以做构造函数,不能使用new命令,因为没有this函数体内没有arguments,可以使用rest参数代替不能用y ...
- canvas 悬浮效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
