POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发,嗯?居然WA了,一定是我姿势不对,我换个编译器交一下,结果还是WA……这就比较尴尬了,最后审题目,发现了,我以为第一个输入的点就是根节点,其实不是,我们还需要找到入度为0的点来处理,这里做一个标记就可以了。还有就是题目的输入稍微有点坑,尤其是第二次输入,题目中说忽略所有空白,这就需要比较好的处理方法了,我采用的是while的方法,当然方法肯定是有很多的。代码如下:
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
#define N 1010
int head[N],qhead[N],n,tot,qtot;
struct Edge{
int to,nxt;
}edge[N*N];
void addedge(int a,int b){
edge[tot].to = b;
edge[tot].nxt = head[a];
head[a] = tot++;
}
Edge query[N*N];
void addquery(int a,int b){
query[qtot].to = b;
query[qtot].nxt = qhead[a];
qhead[a] = qtot++;
}
int vis[N],fa[N],ans[N];
int Find(int x){
return x == fa[x] ? fa[x] : fa[x] = Find(fa[x]);
}
void tarjan(int u){
vis[u] = ;
fa[u] = u;
int v,LCA;
for(int i = qhead[u];i != -;i = query[i].nxt){
v = query[i].to;
if(vis[v]){
LCA = Find(v);
//printf("%d->%d LCA = %d\n",u,v,LCA);
ans[LCA]++;
}
}
for(int i = head[u];i != -;i = edge[i].nxt){
v = edge[i].to;
if(!vis[v]){
tarjan(v);
fa[v] = u;
}
}
return ;
}
int main(){
int a,b,m,num,anc,mark[N];
while(~scanf("%d",&n)){
tot = ;
memset(head,-,sizeof(head));
memset(mark,,sizeof(mark));
for(int i = ;i <= n;i++){
scanf("%d:(%d) ",&a,&num);
for(int j = ;j < num;j++){
scanf("%d",&b);
mark[b] = ;
addedge(a,b);
}
// for(int j = head[a];j != -1;j = edge[j].nxt){
// printf("%d->%d\n",a,edge[j].to);
// }
fa[i] = i;
}
for(int i = ;i <= n;i++){
if(!mark[i]){
anc = i;
break;
}
}
memset(qhead,-,sizeof(qhead));
qtot = ;
scanf("%d",&m);
// getchar();
char ch; int cnt = ;
while(~scanf("%c",&ch)){
if(ch == '('){
scanf("%d %d)",&a,&b);
addquery(a,b);
addquery(b,a);
cnt++;
}
if(cnt==m) break;
}
memset(vis,,sizeof(vis));
memset(ans,,sizeof(ans));
tarjan(anc);
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】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- poj 1470 Closest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- POJ 1470 Closest Common Ancestors LCA题解
本题也是找LCA的题目,只是要求多次查询.一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 由于查询的数据会 ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- 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 ...
随机推荐
- UILabel 的属性设置
.设置字体样式(加粗) label.font = [UIFont boldSystemFontOfSize:30]; 6.设置字体类型 label.font = [UIFont fontWithNam ...
- 8.17HTML 标签
1.HTML body属性: bgbgcolor 页面背景色 text 文字颜色 topmargin 上页面边距 leftmargin 左 rig ...
- 模板singleton模式的C++实现
模板singleton模式的C++实现 近期回过头整理了一下singleton模式,看了别人写的关于singleton的介绍.发现这个singleton模式虽然简单,但要写一个稳定/线程安全/泛型的模 ...
- ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别
Runnable任务没有返回值,而Callable任务有返回值.并且Callable的call()方法只能通过ExecutorService的submit(Callable <T> tas ...
- WinDbg抓取程序报错dump文件的方法
程序崩溃的两种主要现象: a. 程序在运行中的时候,突然弹出错误窗口,然后点错误窗口的确定时,程序直接关闭 例如: “应用程序错误” “C++错误之类的窗口” “程序无响应” “假死”等 此种崩溃特点 ...
- LoadRunner性能测试指标分析
Memory: ·Available Mbytes 简述:可用物理内存数.如果Available Mbytes的值很小(4 MB或更小),则说明计算机上总的内存可能不足,或某程序没有释放内存. 参考值 ...
- redis运维的一些知识点
恰好看到一些redis需要主要的东西 记下 供参考 原文地址 http://hi.baidu.com/ywdblog/item/1a8c6ed42edf01866dce3fe3 最近在线上实际使用了一 ...
- 用php和imagemagick来处理图片文件的上传和缩放处理
啥也不说,直接上代码,大家可以自行添加增加水印功能: <?php /** * * @author zhao jinhan * @date 2014年1月13日11:54:30 * @email ...
- philosophy
Even though the UNIX system introduces a number of innovative programs and techniques, no single pro ...
- 萝卜德森的sublime笔记中文翻译版
我已经使用subliem编辑器版本2接近2个月了,并且我在其中找到了一堆有用的技巧.我发觉应该写下这些技巧,为那些对此感兴趣的人们.我会尽力的详细描述,那些看起来像魔法一样的东西,因为很多非常“酷”的 ...