POJ 1470 Closest Common Ancestors
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.
Source |
-----------------------------------------------------------------------
LCA
采用 Tarjan 离线 LCA 算法比较方便
注意读入细节
-------------------------------------------------------------------------
#include <cstdio>
#include <vector>
#include <cstring>
#define pb push_back using namespace std;
const int N();
vector<int> q[N], g[N];
int par[N], ans[N], col[N];
int find(int u){return par[u]==u?u:find(par[u]);}
void dfs(int u, int f){
col[u]=-;
for(int i=; i<q[u].size(); i++){
int &v=q[u][i];
if(col[v]==-) ans[v]++;
else if(col[v]==) ans[find(v)]++;
else q[v].pb(u);
}
for(int i=; i<g[u].size(); i++){
int &v=g[u][i];
dfs(v, u);
}
col[u]=;
par[u]=f;
}
int main(){
//freopen("in", "r", stdin);
int n, m, u, v;
for(;~scanf("%d", &n);){
for(int i=; i<=n; i++) g[i].clear(), q[i].clear();
memset(par, , sizeof(par));
for(int i=; i<n; i++){
scanf("%d:(%d)", &u, &m);
while(m--){
scanf("%d", &v);
par[v]=u;
g[u].pb(v);
}
}
scanf("%d", &m);
while(m--){
scanf(" (%d%d)", &u, &v);
q[u].pb(v);
}
int rt;
for(rt=; par[rt]; rt=par[rt]);
for(int i=; i<=n; i++) par[i]=i;
memset(ans, , sizeof(ans));
memset(col, , sizeof(col));
dfs(rt, rt);
for(int i=; i<=n; i++) if(ans[i]) printf("%d:%d\n", i, ans[i]);
}
}
POJ 1470 Closest Common Ancestors的更多相关文章
- 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】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- 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, 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
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
- POJ - 1470 Closest Common Ancestors(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
随机推荐
- jsp 微信公众平台 token验证(php、jsp)(转载)
微信公众平台现在推出自动回复消息接口,但是由于是接口内容用的是PHP语言写的,很多地方操作起来让本人这个对java比较熟悉的小伙很别扭,所以仿照PHP的接口代码做了一套jsp语言编写的接口. 首先先把 ...
- EventBus (三) 源码解析 带你深入理解EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...
- JavaScript---基本语法
字符串方法:str.lengthstr.charAt(i):取字符串中的某一个;str.indexOf('e');找第一个出现的位置;找不到返回-1;str.lastIndexOf('e'):找最后一 ...
- javascript中的浅复制和深复制
//浅复制:实现基本类型的复制没有问题,但是复制的是引用类型的话,则修改child将会修改parent function extend(parent,child){ var child = child ...
- DevExpress主从表 按组分页一组不足一页为一页--以此记录
本文的主要是说明Dev的报表的主从表,主从表的每一组显示在一页,当一组超出一页,第二页只显示第一组的. 一.每上报表设置图 简单设计图如上 二.后台代码 报表页代码 public partial cl ...
- C++函数内存占用
一个类的对象中是没有关于普通成员函数的指针的slot,只有成员变量还有虚表指针,类的成员函数的代码定义在PE文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码:代码运行时所需要的内存, ...
- 完成一个MVC+Nhibernate+Jquery-EasyUI信息发布系统
一.最近学习了Jquery-EasyUI框架,结合之前用过的MVC3+Nhibernate做一个信息发布系统,对工作一年半的自己做一个总结吧!(也正好 供初学者学习!) 二.先上截图(系统简介),让大 ...
- [C#]動態叫用Web Service
http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/22/8139.aspx 摘要 Web Service對大家來說想必都不陌生,也大都了解Web S ...
- QuickFIX/J常见问题汇总
最近在搞QuickFIX/J,网上的资料不算很多,遇到一些简单的问题都需要google一阵才能找到解决方法,因此做点记录: 错误:Rejecting invalid message: quickfix ...
- Activiti系列: 如何在web中使用activiti和sql server
最近要开始使用activiti结合原有的系统来开发一个专业领域内的业务管理软件,以下记录了第一次搭建该开发平台过程中所遇到的各种问题,备忘. 一.按照如下方式新建一个web工程 之所以要用 ...