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的函数节流(throttle)
什么是函数节流? 介绍前,先说下背景.在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(其核心就是绑定mousemove),这种事件有一个特点,就是用户不必特地捣乱,他在 ...
- Python中字典dict
dict字典 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现 # 字典的创建 # 创建空字典1 d = {} print(d) # 创建空字典2 d = dict() print(d) ...
- Spring声明式事务配置
1.首先在/WEB-INF/applicationContext.xml添加以下内容: <!-- 配置事务管理器 --> <bean id="transactionMana ...
- Vivox9怎么录制屏幕
手机怎么录屏是很多手机党一直提出的问题,而且经常发生录制的视频没有声音的现象,现在就给大家推荐一款软件,不仅能完美的录制视频,而且还可以完整的将视频声音录制下来,下面看看Vivox9怎么录制屏幕吧! ...
- [Python] wxPython 基本控件 (转)
转自:http://www.cnblogs.com/wangjian8888/p/6028777.html 一.静态文本控件 wx.StaticText(parent, id, label, pos= ...
- MySQL5.7 搭建主从同步
性能更好的新服务器申请下来了,我们决定在2台新服务器上使用mysql5.7,并且使用主从同步.读写分离架构,很不幸这个任务落到了我的头上.读写分离是在业务代码中实现的,在此不做详述,介绍一下我搭建My ...
- POST 400 的一次遭遇
使用Springboot开发接口的过程中,使用POST接收提交的数据. @PostMapping("/input") public void inputData(@RequestB ...
- openstack Ocata版本 python
from keystoneauth1.identity import v3 from keystoneauth1 import session from novaclient import clien ...
- django 下载文件
方法一. from django.http import StreamingHttpResponse def big_file_download(request): # do something... ...
- FileInputStream、FileReader、FileWriter和File
FileInputStream提供了对文件的字节读取 用于读取诸如图像数据之类的原始字节流 如:FileInputStream fis=new FileInputStream(new Fi ...