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 ...
随机推荐
- bat 批处理获取时间语法格式
bat 批处理获取时间语法格式 取年份:echo %date:~0,4% 取月份:echo %date:~5,2% 取日期:echo %date:~8,2% 取星期:echo %date:~10 ...
- Python 基于Python从mysql表读取千万数据实践
基于Python 从mysql表读取千万数据实践 by:授客 QQ:1033553122 场景: 有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...
- Wu反走样算法绘制圆(C++/MFC实现)
Wu反走样圆 原理:参考Bresenham算法,在主位移过程中计算出离理想圆最近的两个点,赋予不同的亮度值,绘制像素点即可! MFC 中CXXXView类中添加函数: //Wu算法画反走样圆 void ...
- git 入门教程之个性化 git
前情概要 初识 git 时,我们就已经接触过 git 的基本配置,使用 git config 命令配置用户名和邮箱: # 配置当前项目(`local`)的用户名(`snowdreams1006`) g ...
- jsfiddle 使用教程
最近有许多的Css 3 demo,因此为了方便查阅,就将demo部分放在jsfiddle ,方便日后翻阅. 这是 JSFIDDLE 的官网文档,都是英文,不过对照看还是可以的:官方文档 HTML区域: ...
- Python minidom模块(DOM写入和解析XML)
一.DOM写XML文件 #导入minidom from xml.dom import minidom # 1.创建DOM树对象 dom=minidom.Document() # 2.创建根节点.每次都 ...
- 扫码下单使用FAQ
1.适用情景:扫码点餐支付宝支付报错 解决方案:1.检查主账号上口碑授权是否失效.(重新授权) 2.检查主账号上的PID是否绑定.(绑定PID) 注意:1.支付宝扫码进行的扫码下单支持直连支付宝和蚂蚁 ...
- DBA思考系列——学会拒绝不合理的需求
DBA思考系列--学会拒绝不合理的需求 一直以来,个性都比较随意,一般很少拒绝开发人员的一些需求(有点老好人的感觉). 这点一直被老大诟病,也一直在反省!最近又有一件事情,让我觉得:应该学会拒绝不 ...
- 获取spring applicationcontext数据连接connection
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ...
- weblogic报错----Received exception while creating connection for pool "TDMSKD": The Network Adapter could not establish the connection
<2017-8-16 上午08时58分37秒 CST> <Info> <WebLogicServer> <BEA-000377> <Startin ...