poj-1330(暴力写的lca)
一看就是lca的板子题
然而
(写这个的时候我忘了怎么写lca)
于是我就试着写暴力了
本以为会tle结果e了一次后居然a掉了
开心到起飞.嘿嘿嘿
但还是格式输出错误了一次而且在ce之前也de了一会儿(sdqxt太蒻了)
sd错误如下:
1. 边的编号和点的编号弄混了
2. 一开始直接写成i <= deep[x] – deep[y]
于是导致循环少了好几次
(这个问题我在博客了写了好多遍,可我还是错qwq..)

3. 但我真正提交的错误是,\n被我写成'空格'了(纯是没在意这事啊)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; int fa[],deep[],head[],nxt[],to[],cnt;
bool vis[]; inline int read()
{
int sum = ,p = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-')
p = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
(sum *= ) += ch - '';
ch = getchar();
}
return sum * p;
} void dfs(int o)
{
for(int i = head[o];i;i = nxt[i])
{
deep[to[i]] = deep[o] + ;
dfs(to[i]);
}
} void add(int x,int y)
{
fa[y] = x;
nxt[++cnt] = head[x];
head[x] = cnt;
to[cnt] = y;
} void llca(int x,int y)
{
if(deep[x] < deep[y])//始终让x为最深的
swap(x,y);
int qwq = deep[x] - deep[y];
for(int i = ;i <= qwq;i++)
x = fa[x];
if(x == y)
printf("%d\n",y);
else
{
while(x != y)
{
x = fa[x];
y = fa[y];
}
printf("%d\n",y);
}
} int main()
{
int t,n;
t = read();
while(t--)
{
n = read();
cnt = ;
memset(fa,,sizeof(fa));
memset(deep,,sizeof(deep));
memset(head,,sizeof(head));
memset(nxt,,sizeof(nxt));
memset(vis,,sizeof(vis));
memset(to,,sizeof(to));
for(int i = ;i < n;i++)
{
int a= read(),b = read();
vis[b] = true;
add(a,b);
}
int c = read(),d = read();
for(int i = ;i <= n;i++)//找根节点
if(!vis[i])
{
deep[i] = ;
fa[i] = i;
dfs(i);
break;
}
llca(c,d);
}
return ;
}
poj-1330(暴力写的lca)的更多相关文章
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
随机推荐
- JS怎样实现图片的懒加载以及jquery.lazyload.js的使用
在项目中有时候会用到图片的延迟加载,那么延迟加载的好处是啥呢? 我觉得主要包括两点吧,第一是在包含很多大图片长页面中延迟加载图片可以加快页面加载速度:第二是帮助降低服务器负担. 下面介绍一下常用的延迟 ...
- MongoDB 3.0 WT引擎参考配置文件
单实例: systemLog: destination: file ###日志存储位置 path: /data/mongodb/log/mongod.log logAppend: true stora ...
- Dynamics 365 Online-Relevance Search
区别于Quick Find,以及Full-Text Quick Find,Dynamics 365 Online有了一个特有的Search功能:Relevance Search.至于为什么是Onlin ...
- 关系数据库标准语言SQL——概述
SQL是一种介于关系代数与关系演算之间的结构化查询语言,其功能并不仅仅是查询.SQL是一个通用的.功能极强的关系数据库语言.SQL(Structured Query Language)结构化查询语 ...
- Android 逆向实战篇(加密数据包破解)
1. 实战背景由于工作需要,要爬取某款App的数据,App的具体名称此处不便透露,避免他们发现并修改加密逻辑我就得重新破解了. 爬取这款App时发现,抓包抓到的数据是加密过的,如图1所示(原数据较长, ...
- Testlink1.9.17使用方法(第十章 关键词使用)
第十章 关键词使用:按关键词来搜索用例 QQ交流群:585499566 一. 创建关键字 单击主页“产品管理”区域-->点击[关键字管理]按钮-->创建一个新的关键字.关键字的内容包括:关 ...
- $.ajax({})方法中的回调函数beforeSend,success,complete,error使用示例
在与后台交互的时候,经常使用到jquery的$.ajax()方法来请求数据.回调函数用的比较多的是success,但是beforeSend.complete.error函数也是很有用的.下面是使用例子 ...
- java----堆区、方法区和栈区
堆区:只存放类对象,线程共享: 方法区:又叫静态存储区,存放class文件和静态数据,线程共享; 栈区:存放方法局部变量,基本类型变量区.执行环境上下文.操作指令区,线程不共享; class A { ...
- 转:Log Explorer使用说明恢复被误删除的数据
一.介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复.你可以浏览日志.导出数据.恢复被修改或者删除的数据(包括执行过update,delete,drop和trunca ...
- AI学习---数据读取&神经网络
AI学习---数据读取&神经网络 fa