POJ 1470 Tarjan算法
裸的LCA,读入小坑。Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b
傻傻的用了邻接矩阵。。。很慢啊,1100多ms。
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
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
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
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
Huge input, scanf is recommended.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char jy;
bool vis[999];
int q,m,jyy,first[999],ans[999],next[999999],tot,qu[999][999],v[999999],n,k,xx,yy,root,f[999];
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
void Tarjan(int x)
{
f[x]=x;
for(int i=first[x];~i;i=next[i])
{
Tarjan(v[i]);
f[find(v[i])]=x;
}
vis[x]=1;
for(int i=1;i<=n;i++)
{
if(vis[i]&&qu[x][i])
{
ans[find(i)]+=qu[x][i];
qu[x][i]=qu[i][x]=0;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(qu,0,sizeof(qu));
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
memset(first,-1,sizeof(first));
tot=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&q);
while(scanf("%c",&jy)&&jy!='(');
scanf("%d",&m);
while(scanf("%c",&jy)&&jy!=')');
for(int i=1;i<=m;i++)
{
scanf("%d",&v[tot]);
vis[v[tot]]=1;
next[tot]=first[q];
first[q]=tot++;
}
}
for(int i=1;i<=n;i++)
if(!vis[i])
{
root=i;
break;
}
memset(vis,0,sizeof(vis));
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
while(scanf("%c",&jy)&&jy!='(');
scanf("%d%d",&xx,&yy);
qu[xx][yy]++;
qu[yy][xx]=qu[xx][yy];
while(scanf("%c",&jy)&&jy!=')');
}
Tarjan(root);
for(int i=1;i<=n;i++)
if(ans[i])
printf("%d:%d\n",i,ans[i]);
}
}
POJ 1470 Tarjan算法的更多相关文章
- POJ 1236 Tarjan算法
这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...
- POJ - 2553 tarjan算法+缩点
题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...
- 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(离线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 ...
- 【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17104 Accepted: 4594 Description In o ...
随机推荐
- 【转载】Linux 通过mount -o loop 配置本地.iso镜像为yum源(yum仓库)
原文地址:https://www.jb51.net/os/RedHat/2682_all.html 如果拷贝到本地,可以使用mount mount fileName mountPoint -o loo ...
- tomcat 编码设置
在Tomcat8.0之前的版本,如果你要向服务器提交中文是需要转码的(如果你没有修改server.xml中的默认编码),因为8.0之前Tomcat的默认编码为ISO8859-1. POST方式提交 r ...
- JavaScript 复杂判断的优雅写法
JavaScript 复杂判断的优雅写法 <div> <input type="button" name="btn" value=" ...
- js调用ro的webservice
Enabling JavaScript Access on the Server Drop the JavaScriptHttpDispatcher component onto the server ...
- 用Grails写单元测试
新的领域,多练练,这样写出的程序,确实坚固些. 也要理解集成测试与数据库相关,单元测试与类方法有关. 如果测试文件没有建立,按如下操作: Unit tests are generated automa ...
- 虚拟机+centOS挂载ISO步骤
https://blog.csdn.net/u010612373/article/details/52240447
- ZooKeeper环境搭建(单机/集群)(转)
前提: 配置文件主要是在$ZOOKEEPER_HOME/conf/zoo.cfg,刚解压时为zoo_sample.cfg,重命名zoo.cfg即可. 配置文件常用项参考:http://www.cnbl ...
- HPC2013小节
对于高性能计算,三个分支能耗.高性能.容错.下面我对会议的主要内容作一个小节,很多问题也是不求甚解. 下面针对大会内容,我主要总结如下,会有了解不周的地方,欢迎讨论:大会主要报告分成3个方向,1.基础 ...
- Python开发工具安装
v阅读目录 v写在前面 v基本概念 vWindows搭建python开发环境 v从Hello World开始 v博客总结 v博客前言 从大学开始玩python到现在参加工作,已经有5年了,现在的公司是 ...
- nyoj 1238 最少换乘 (河南省第八届acm程序设计大赛)
题目1238 题目信息 执行结果 本题排行 pid=1238" style="text-decoration:none; color:rgb(55,119,188)"&g ...