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培训机构?
Linux培训已经成为入门Linux的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Linux培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部分同学 ...
- hdu 4876
ZCC loves cards Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- java ArrayList去重复值
public static List removeDuplicateWithOrder(List list) { Set set = new HashSet(); List newList = new ...
- vue开发规范
一.简介 团队合作中规范文档是必须的,在多人合作的项目只有定义好一定的编码规范才会使得开发井井有序,代码一目了然,下边将谈一下个人对vue使用规范的一些看法. 二.规范案例 1.组件命名 组件文件名应 ...
- (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】
已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...
- 清北学堂模拟赛d6t1 角谷猜想
分析:不用删数字,我们考虑加入数字,维护一个栈,把不是4和7的数加进去,遇到3看栈顶是不是1,是的话弹出来就可以了. #include <bits/stdc++.h> #define N ...
- nyoj_478_月老的烦恼(1)_201312101248
月老的烦恼(1) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 月老最近遇到了一个很棘手的问题,就是“剩男”“剩女”急速增长,而自己这边又人手不足 ...
- gap lock/next-key lock浅析Basic-Paxos协议日志同步应用
http://www.cnblogs.com/renolei/p/4673842.html 当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将 ...
- django 和 mysql的一次troubleshooting
下面是一次用django连接mysql的经历,记录下来也许以后会有帮助. 首先是用django的./manage.py syncdb 去连接mysql -bash-3.2$ ./manage.py s ...
- 2.6-NAT
2.6-NAT 网络地址转换协议NAT(Network Address Translation): 交换和远程都要用,先上什么就放在哪一块讲,具体来说NAT还是属于远程的. ...