Language:
Default
Closest Common Ancestors
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 24653   Accepted: 7648

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 
vertex:(nr_of_successors) successor1 successor2 ... successorn 
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ...

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

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
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
(2 3)
(1 3) (4 3)

Sample Output

2:1
5:5

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 ;
}

最新文章

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

热门文章

  1. FTP服务器与客户端的安装与配置
  2. laravel如何实现多用户体系登录
  3. mysql系列-⼀条SQL查询语句是如何执⾏的?
  4. GB2312,GBK和UTF-8的区别
  5. 第二章 Getting started with functional programming
  6. 面试刷题30:SpringBean的生命周期?
  7. iOS 图片加载速度优化
  8. MATLAB GUI设计(1)
  9. Linux 脏数据回刷参数与调优
  10. U - Inviting Friends HDU - 3244(二分答案 + 完全背包)