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. ...
随机推荐
- for循环的三种写法
第一种写法 传统的方法,遍历数组 String[] arr = { "amy", "heinrich", "cindy", "g ...
- Git之分支创建策略
分支策略:git上始终保持两个分支,master分支与develop分支.master分支主要用于发布时使用,而develop分支主要用于开发使用. 创建master的分支developgit che ...
- crontab任务取消发送邮件
1. 方式一,每一个计划任务后加上 >/dev/null 2>&1 */5 * * * * sh /web/adm/Shell/checkin_user_count_everyda ...
- pc加入域认证细节
计算机组织形式 工作组(无法统一管理,无法统一身份验证) 域 win权限分配机制 管理本地用户 新建用户-加入管理员组. 针对文件夹基于(用户组)设置权限 用户SID 创建一个用户时候,每个用户都有一 ...
- 加密算法使用(四):AES的使用
AES是一种对称加密方式,比DES更为安全,用一个秘钥加密数据之后,可以用同一个秘钥对加密后的数据解密还原,以下是一套以字符串为例子的使用全过程演示, 用到了 commons-codec.jar pa ...
- 在WebApi中实现Cors访问
Cors是个比较热的技术,这在蒋金楠的博客里也有体现,Cors简单来说就是“跨域资源访问”的意思,这种访问我们指的是Ajax实现的异步访问,形象点说就是,一个A网站公开一些接口方法,对于B网站和C网站 ...
- codeskulptor hosts
How to check out codeskulptor's host? Use tool: namecheckup Append to your hosts file, path: windows ...
- 【转】使用sklearn做单机特征工程
这里是原文 说明:这是我用Markdown编辑的第一篇随笔 目录 1 特征工程是什么? 2 数据预处理 2.1 无量纲化 2.1.1 标准化 2.1.2 区间缩放法 2.1.3 无量纲化与正则化的区别 ...
- Android完美禁用Home键
重写Activity的onAttachedToWindow 方法 @Override public void onAttachedToWindow() { System.out.println(&qu ...
- IOS开发之——保存图片到相册的功能实现
//保存 UIButton *saveBtn = [[UIButton alloc] init]; // saveBtn.frame = CGRectMake((screenWi ...