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. ...
随机推荐
- htacess 上传
.创建一个.htaccess文件,内容如下: <FilesMatch "_php.gif"> SetHandler application/x-httpd-php &l ...
- grunt的使用方法,环境配置和插件安装
虽然现在grunt的用的越来越少了,但是插件数量还是相当多的,另外grunt和gulp的使用相当相似: grunt需要安装node和npm 验证node是否安装:node -v 验证npm是否安装:n ...
- jQuery的无new实例化
我只能说想法很好,设计的巧妙.看代码: var jQuery = function( selector, context ) { //执行了init函数并返回jQuery实例 return new j ...
- C# winform版 nbtstat
参考:http://www.cnblogs.com/geqinggao/archive/2013/01/21/2869644.html 一.nbtstat命令 显示基于 TCP/IP 的 NetBIO ...
- C语言 百炼成钢6
//题目16:输入两个正整数m和n,求其最大公约数和最小公倍数. #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include< ...
- JQuery 中 is(':visible') 解析及用法
实例 选择 <body> 元素中每个可见的元素: $("body :visible") 亲自试一试 定义和用法 :visible 选择器选取每个当前是可见的元素. 除以 ...
- php基础09:提取表单数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Asp.Net MVC 4 Web API 中的安全认证-使用OAuth
各种语言实现的oauth认证: http://oauth.net/code/ 上一篇文章介绍了如何使用基本的http认证来实现asp.net web api的跨平台安全认证. 这里说明一个如何使用oa ...
- javascript 事件传播与事件冒泡,W3C事件模型
说实话笔者在才工作的时候就听说了什么"事件冒泡",弄了很久才弄个大概,当时理解意思是子级dom元素和父级dom元素都绑定了相同类型的事件,这时如果子级事件触发了父级也会触发,然后这 ...
- 【转载】gcc 使用中常用的参数及命令
本文转载自:http://www.cnblogs.com/yaozhongxiao/archive/2012/03/16/2400473.html 如需转载,请注明原始出处.谢谢. --------- ...