poj----(1470)Closest Common Ancestors(LCA)
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 15446 | Accepted: 4944 |
Description
Input
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
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
Source
/*poj 1470*/
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
vector<int> tree[maxn],qus[maxn];
int rank[maxn],father[maxn];
bool vis[maxn];
int rudu[maxn];
int lroot[maxn];
int ans[maxn]; void init(int n){
memset(vis,,sizeof(char)*(n+));
memset(rudu,,sizeof(int)*(n+));
memset(lroot,,sizeof(int)*(n+));
memset(ans,,sizeof(int)*(n+));
for(int i=;i<=n;i++){
father[i]=i;
rank[i]=;
tree[i].clear();
qus[i].clear();
}
} int find(int a){
while(a!=father[a])
a=father[a];
return a;
} void Union(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y) return ;
if(rank[x]<rank[y]){
rank[y]+=rank[x];
father[x]=y;
}
else {
rank[x]+=rank[y];
father[y]=x;
}
} void LCA(int u)
{
lroot[u]=u;
//vis[u]=1; 不能放在这里
int len=tree[u].size();
for(int i=;i<len;i++){
LCA(tree[u][i]);
Union(u,tree[u][i]);
lroot[find(u)]=u;
}
vis[u]=;
int ss=qus[u].size();
for(int i=;i<ss;i++){
if(vis[qus[u][i]]){
ans[lroot[find(qus[u][i])]]++;
//return ;
}
}
} int main()
{
int n,m,t,u1,u2;
freopen("test.in","r",stdin);
while(scanf("%d",&n)!=EOF){
init(n);
for(int i=;i<n;i++){
getchar();
scanf("%d:(%d))",&u1,&m);
for(int j=;j<m;j++){
scanf("%d",&u2);
tree[u1].push_back(u2);
rudu[u2]++;
}
}
scanf("%d",&t);
for(int i=;i<t;i++)
{
scanf("%*1s%d%d%*1s",&u1,&u2);
qus[u1].push_back(u2);
qus[u2].push_back(u1);
}
for(int i=;i<=n;i++)
{
if(rudu[i]==)
{
LCA(i);
break;
}
}
for(int i=;i<=n;i++){
if(!=ans[i])
printf("%d:%d\n",i,ans[i]);
}
}
return ;
}
poj----(1470)Closest Common Ancestors(LCA)的更多相关文章
- 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,离线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 ...
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- POJ - 1470 Closest Common Ancestors(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
随机推荐
- vim使用札记
最近开始用vim编辑器了,从最开始的配置到现在慢慢使用,我在这儿会贴出一些我的使用上遇到过的问题和如何解决的方案,留给自己和一些会用到的人看看 1.vim怎么使汇编语法高亮 开始不知道,然后把文件的后 ...
- nancy中的诊断功能面板1
nancyfx中有一项 诊断功能 ,可以查看网站的基本信息和其他相关信息,还包括查看会话信息,请求输出信息等. 假设你已经安装完了nancyfx.现在开始使用诊断功能: 一 安装 在你的Bootstr ...
- FormsAuthentication.HashPasswordForStoringInConfigFile 方法 之研究
摘自:http://time-is-life.cnblogs.com/articles/322523.html 给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码. [C#] ...
- 关于dom ready事件
0.加载完页面,解析完所有标签(不包括执行CSS和JS),并如规范中所说的设置 interactive 和执行每个静态的script标签中的JS,然后触发. 1.没有js,有css,有img,DOMC ...
- JAVA中Response的几种用法(设定时间调整到指定页面 ....... )
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%&g ...
- Java、fileless恶意软件威胁桌面安全
工作原理:用户访问一个受侵的网站,不小心下载了最新类型的恶意软件.如果你的杀毒软件运行良好的话,就会阻止下载,至少能够检测到并隔离硬盘上的入侵文件.但是如果硬盘上没有文件监测呢?如果恶意软件只入侵内存 ...
- shift移动变量
1.移动变量 脚本 sh05.sh #!/bin/bash # Program # Program shows the effect of shift function # History: # // ...
- tomcat PermGen space
centos: 修改Tomcat中的catalina.sh文件.--用了这个 在catalina.sh文件中,找到cygwin=false,在这一行的前面加入参数,具体如下 # vim TOMCAT_ ...
- poj1755Triathlon(半平面交)
链接 根据题意可以设三段路程分别为A,B,C 那么总时间t = A/V+B/U+C/W. 这样根据时间大小关系可以跟其余n-1个联立形成n-1个方程. 化简后为A(1/vj-1/vi)+B(1/uj- ...
- Python学习(10)元组
目录 Python 元组 访问元组 修改元组 删除元组 元组运算符 元组索引,截取 无关闭分隔符 元组内置函数 Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组 ...