Closest Common Ancestors POJ 1470
2024-09-05 16:27:02
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 ;
}
最新文章
- sql语句获取今天、昨天、近7天、本周、上周、本月、上月、半年数据
- delphi 控件大全(确实很全)
- PySe-003-Se-WebDriver 启动浏览器之一 - Firefox
- VMware中安装CentOS7网络配置静态IP地址,常用配置和工具安装
- haskell rust相关文章
- JPA学习---第一节:JPA详解
- [LeetCode] 76. Minimum Window Substring 解题思路
- [笔记]SD卡相关资料
- ModelAndView解析
- UML-状态图,顺序图,活动图
- 关于DOM的理解
- C# linq左连接与分组
- Struts2与Ajax数据交互
- Elimination Game题解
- 小程序之hover-class
- b2b
- 【机器学习基础】SVM实现分类识别及参数调优(二)
- 【HTML5】HTML5的自学路线
- atitit.js&#160;与c#&#160;java交互html5化的原理与总结.doc
- STL的集合set