Cloesest Common Ancestors
Cloesest Common Ancestors
题目大意:给出一个n个节点的树,m组询问求两点LCA。
注释:n<=900.
想法:这题一看,我去,这不傻题吗?一看读入方式,完了,懵逼了... ...这题是考读入啊一大堆乱七八糟的东西,真正有用的只有里面的数... ...然后,我学了两个比较有用的东西。
1.scanf("%*d");其中,*表示读完就删,所以可以用来处理本题。
2.强大的nc+read(),在此不做介绍。
总之,初学LCA,这是另一道朴素的LCA,用桶记录即可。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1000
using namespace std;
int to[N],nxt[N],tot,fa[N],deep[N],barrel[N];
int head[N];
bool v[N];
// inline char nc()
// {
// static char buf[100000],*p1,*p2;
// return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline void read(int &x)
// {
// x=0;char c=nc();
// while(!isdigit(c))c=nc();
// while(isdigit(c))x=x*10+c-'0',c=nc();
// }
inline void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x,int pre,int step)
{
fa[x]=pre;
deep[x]=step;
for(int i=head[x];i;i=nxt[i])
{
dfs(to[i],x,step+);
}
}
inline int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
while(deep[x]>deep[y]) x=fa[x];
while(x!=y) x=fa[x],y=fa[y];
return x;
}
int main()
{
int n,m;
int a,b;
char s[];
while(~scanf("%d",&n))
{
tot=;
memset(head,,sizeof(head));
memset(v,false,sizeof(v));
memset(barrel,,sizeof(barrel)); // scanf("%d",&n);
for(int i=;i<=n;i++)
{
// read(a);
scanf("%*[^0-9]%d",&a);
// read(m);
scanf("%*[^0-9]%d",&m);
for(int i=;i<=m;i++)
{
scanf("%*[^0-9]%d",&b);
add(a,b);
v[b]=;
}
}
int root;
for(int i=;i<=n;i++)
{
if(!v[i])
{
root=i;
break;
}
}
dfs(root,root,);
scanf("%*[^0-9]%d",&m);
for(int i=;i<=m;i++)
{
scanf("%*[^0-9]%d",&a);
scanf("%*[^0-9]%d",&b);
barrel[lca(a,b)]++;
}
for(int i=;i<=n;i++)
{
if(barrel[i]!=) printf("%d:%d\n",i,barrel[i]);
}
scanf("%*[^0-9]");
}
}
小结:LCA的朴素算法还是很有用的啊... ...
错误:1.在读取链式前向星时一定要记得是i=nxt[i]而不是nxt[i]。
2.最后仍然会剩余一个括号,别忘记了它会影响下一组数据。
Cloesest Common Ancestors的更多相关文章
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14698 Accept ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- 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(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
随机推荐
- javascript DOM document对象
document对象代表整个html文档 用来访问页面所有元素最复杂的一个dom对象 也是window对象的一个子对象. 对于dom编程中,一个html就会当成一个dom树dom会把所有的html元素 ...
- Linux以GB显示内存大小
Linux以GB显示内存大小 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -g total used free shared buffers ca ...
- html中的div span和frameset框架标签
Div和span 1. div独占一层,由div九不允许有别的. 2. span标签不是独自占用一行,span一般用来设置字体. 框架标签: 什么是框架标签,就是把一个页面分成很多 ...
- babel-runtime和babel-polyfill两者区别优缺点
先说两种方式的原理: babel-polyfill 使用场景 Babel 默认只转换新的 JavaScript 语法,而不转换新的 API.例如,Iterator.Generator.Set.Maps ...
- EntityFramework Core 2.0 Explicitly Compiled Query(显式编译查询)
前言 EntityFramework Core 2.0引入了显式编译查询,在查询数据时预先编译好LINQ查询便于在请求数据时能够立即响应.显式编译查询提供了高可用场景,通过使用显式编译的查询可以提高查 ...
- 实例 find
2011/09/08 12:00 时间开始找一天內的,会列出 2011/09/07 12:00 ~ 2011/09/08 12:00 时间內的文件3天前被改动过的文件 (前第三天以前 → 2011/0 ...
- javascript学习记录-2-18
对象定义的几种方法: var person=new Object(); person.name="111"; person.age=22; 或 var person={ na ...
- 【BZOJ2442】修建草坪(动态规划,单调队列)
[BZOJ2442]修建草坪(动态规划,单调队列) 题面 权限题..洛谷 题解 设\(f[i]\)表示前\(i\)个里面选出来的最大值 转移应该比较显然 枚举一个断点的位置,转移一下就好 \(f[i] ...
- 【BZOJ2729】【HNOI2012】排队(组合数学)
不想弄题面了... 题解 做这道题目我真的好蠢... 好容易的数学题目 很明显自己写高精度吧...(不解释了) 剩下的如何计算. 要有两类情况 ①老师之间有男生 那么,这种情况下,直接插空就行了 先把 ...
- Django使用Celery异步任务队列
1 Celery简介 Celery是异步任务队列,可以独立于主进程运行,在主进程退出后,也不影响队列中的任务执行. 任务执行异常退出,重新启动后,会继续执行队列中的其他任务,同时可以缓存停止期间接收 ...