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 ...
随机推荐
- 金融和IT的区别
在进入金融圈之前, 我写了十五年的代码, 在San Francisco Bay Area(也就是中国人所说的硅谷)工作过两三年. 去年因为Fintech和香港.NET俱乐部的缘故, 我接触了私人银行和 ...
- CF1324C Frog Jumps 题解
原题链接 简要题意: 现在河面上有 \(n+2\) 块石头,编号 \(0\) 到 \(n+1\),\(1\)~\(n\) 块石头每块上有一个方向,如果是 \(L\),那么青蛙到这块石头上之后只能往左跳 ...
- Java基础语法(6)-注释
title: Java基础语法(6)-注释 blog: CSDN data: Java学习路线及视频 用于注解说明解释程序的文字就是注释. 提高了代码的阅读性:调试程序的重要方法. 注释是一个程序员必 ...
- 《 OO第一作业周期(前四周)总结 》
作为一名软件工程的大学生,很高兴能够以这样一种方式,实现对博客编写零的突破.专业课老师也介绍了编写博客给我们带来的帮助,听了以后,我感觉到了培养出写博客的习惯,是一件多么有意义的事! 话不多说,让我们 ...
- MATLAB——颜色梯度显示
一.colormap函数 figure surf(peaks) k=; mycolor=spring(k); colormap(mycolor); spring是颜色的分类.spring(k)生成k个 ...
- 1.Metasploit介绍与基本命令
Metasploit体系框架介绍 Metasploit是目前世界上领先的渗透测试工具,也是信息安全与渗透测试领域最大的开源项目之一.它彻底改变了我们执行安全测试的方式. Metasploit之所以流行 ...
- B - 来找一找吧 HihoCoder - 1701(排列组合 + 同余差值相同)
这次到渣渣问桶桶了... 准备给你n个数a1, a2, ... an,桶桶你能从中找出m个特别的整数吗,我想让任意两个之差都是k的倍数. 请你计算有多少种不同的选法.由于选法可能非常多,你只需要输出对 ...
- P1006 传纸条(二维、三维dp)
P1006 传纸条 输入输出样例 输入 #1 复制 3 3 0 3 9 2 8 5 5 7 0 输出 #1 复制 34 说明/提示 [限制] 对于 30% 的数据,1≤m,n≤10: 对于 100% ...
- 新建基于STM32F103ZET6的工程-HAL库版本
1.STM32F103ZET6简介 STM32F103ZET6的FLASH容量为512K,64K的SRAM.按照STM32芯片的容量产品划分,STM32F103ZET6属于大容量的芯片. 2.下载HA ...
- vue引入echart Error in mounted hook: "ReferenceError: myChart is not defined" found in
解决办法: // 实例化echarts对象 var/let myChart = echarts.init(this.$refs.myChart)