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的思想,从根结点开始形成一棵深搜树,非常好 ...
随机推荐
- CSS3选择器 :nth-child(n) 详解
CSS3 :nth-child(n): http://demo.doyoe.com/css3/nth-child(n)/ 浏览器参照基准:IE9, Firefox, Chrome, Safari, O ...
- 起点CG原创教程——digicel.FlipBook.6教程
http://blog.renren.com/blog/262793929/473221163
- C++ Redis mset 二进制数据接口封装方案
C++ Redis mset 二进制数据接口封装方案 需求 C++中使用hiredis客户端接口访问redis: 需要使用mset一次设置多个二进制数据 以下给出三种封装实现方案: 简单拼接方案 在r ...
- python_way day14 HTML
python_way day 14 HTML 一,标签 二.特殊字符 三,css <!DOCTYPE html> <html lang="en"> < ...
- WinForm 弹框确认后执行
if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIc ...
- kakfa的常用命令总结
Kafka的版本间差异较大,下面是0.8.2.1的操作方法 首先cd到kafaka的bin目录下; #step1启动zookeeper服务 nohup bin/zookeeper-server-s ...
- C#形参,实参,值传递参数,引用传递参数,输出参数,参数数组的学习
1)形参 形参顾名思义就是形式上的参数,不是实际的参数,它代替实际传入方法的值.在方法体代码中代表了值本身参与运算.形参定义于参数中,它不同于方法体内局部变量,因为是一个变量,在它的作用域内不允许存在 ...
- DOM解析XML练习
首先以XML文件存储数据,格式如下(作为数据库) exam.xml <?xml version="1.0" encoding="UTF-8" standa ...
- [转] Android获取Manifest中<meta-data>元素的值
转自: http://www.2cto.com/kf/201303/194824.html android 开发中: 在AndroidManifest.xml中,<meta-data> ...
- fFFmpeg 命令、案例、测试集中营
gitbook: https://www.gitbook.com/book/xdsnet/other-doc-cn-ffmpeg/details ffmpeg [全局选项] {[输入文件选项] -i ...