裸的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算法的更多相关文章

  1. POJ 1236 Tarjan算法

    这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...

  2. POJ - 2553 tarjan算法+缩点

    题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...

  3. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  4. POJ - 1470 Closest Common Ancestors(离线Tarjan算法)

    1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...

  5. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  6. 【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  7. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  8. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  9. 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 ...

随机推荐

  1. Boolean对象与Boolean原始值的区别

    我们先来看下面这道题: var x = new Boolean(false); if (x) {   alert('hi'); } var y = Boolean(0); if (y) {   ale ...

  2. MHA的MySQL高可用方案实战

    功能: 1)master的故障切换(keepalived VIP的飘移) 2)主从复制角色的提升和重新转向 其中master 对外提供写服务,备选master2(实际的slave提供读服务,slave ...

  3. HTML 符号实体

    带有实体名称的 ASCII 实体 结果 描述 实体名称 实体编号 " quotation mark " " ' apostrophe  &apos; ' & ...

  4. 【XSY3413】Lambda - 造计算机初步——邱奇-图灵论题与lambda演算

    题意: 关于邱奇-图灵论题的一点思考 这道题起源于计算机科学史上一个非常著名的问题——邱奇-图灵论题,这个论题是可计算性理论的基石,关于它的思考与证明几乎贯穿了整个计算机科学史,涵盖了数学.算法理论. ...

  5. vue 底部bottomnav

    <template> <div id="foot"> <div class="tabBar"> <div class= ...

  6. 【codeforces 755F】PolandBall and Gifts

    [题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...

  7. hibernate之单表映射

    目录 第一章 Hibernate初识 1-1 课程介绍 1-2 什么是ORM 1-3 Hibnerate简介 1-4 开发前的准备 1-5 编写第一个Hibernate例子 1-6 创建hiberna ...

  8. HTML-class与id的区别及应用

    在样式表定义一个样式的时候,可以定义id也可以定义class. 1.在CSS文件里书写时,ID加前缀"#":CLASS用"." 如只能用id #nav { wi ...

  9. SSH整合开发时Scope为默认时现象与原理

    1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...

  10. [Jest] Write data driven tests in Jest with test.each

    Often, we end up creating multiple unit tests for the same unit of code to make sure it behaves as e ...