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的思想,从根结点开始形成一棵深搜树,非常好 ...
随机推荐
- java读取文件多种方法
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用 ...
- zoj 2107&&hdu 1007最近点对问题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds ...
- FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼)
FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- How to build a GUI in ROS with Qt / C++
p { margin-bottom: 0.1in; direction: ltr; line-height: 120%; text-align: left; widows: 2; orphans: 2 ...
- Python面向对象入门
http://www.math.pku.edu.cn/teachers/qiuzy/ds_python/courseware/ 这本书的第二章写的是抽象数据类型和Python类 以前从没想过认真的去写 ...
- unity3d vs2012
Unity3D自带的MonoDevelop编辑器无论是js还是c#代码提示都很差,很诡异的就是变量名和方法名有的时候提示有的时候不提示.不过用Visual Studio代替MonoDevelop这个问 ...
- 顾维灏谈百度地图数据采集:POI自动处理率达90%
顾维灏谈百度地图数据采集:POI自动处理率达90% 发布时间:2015-12-21 22:37 来源:cnsoftnews.com 作者: 百度地图还创新研发高精地 ...
- CDN学习笔记二(技术详解)
一本好的入门书是带你进入陌生领域的明灯,<CDN技术详解>绝对是带你进入CDN行业的那盏最亮的明灯.因此,虽然只是纯粹的重点抄录,我也要把<CDN技术详解>的精华放上网.公诸同 ...
- C# ?(问号)的三个用处(转载)
public DateTime? StatusDateTime = null; 脑子便也出现个问号,这是什么意思呢?网上搜下,总结如下: 1. 可空类型修饰符(?): 引用类型可以使用空引用表示一个不 ...
- PPPOE协议
PPPOE协议 PPPOE的全称为PPP Over Ethernet,用于实现PPP在以太网上的传输.它要求通信双方是点到点的关系,不适于广播型的以太网和一些多点访问型网络. 一.PPPOE的作用 将 ...