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的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  2. [最近公共祖先] POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accept ...

  3. POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accept ...

  4. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  5. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  6. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  7. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  8. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  9. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

随机推荐

  1. 不使用spring的情况下用java原生代码操作mongodb数据库的两种方式

    由于更改了mongodb3.0数据库的密码,导致这几天storm组对数据进行处理的时候,一直在报mongodb数据库连接不上的异常.   主要原因实际上是和mongodb本身无关的,因为他们改的是配置 ...

  2. R语言︱用excel VBA把xlsx批量转化为csv格式

    笔者寄语:批量读取目前看到有以下几种方法:xlsx包.RODBC包.批量转化成csv后读入.本章来自博客:http://www.cnblogs.com/weibaar/p/4506144.html 在 ...

  3. Linux显示历史记录

    Linux显示历史记录 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ history 1 uname -a 2 lsusb 3 df -h 4 ps -A 5 ...

  4. HTML中设置背景图的两种方式

    HTML中设置背景图的两种方式 1.background    background:url(images/search.png) no-repeat top; 2.background-image ...

  5. BUAA软工第0次作业

    第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答) 我在大学之前甚至连一个萌新都算不上,根本没有任何一点计算机专业的基础. 因此在进入大学之前,计算机对于我 ...

  6. Alice and Bob HDU - 4268

    Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...

  7. Linux之权限管理

    一.文件基本权限 1) 基本权限的修改 第一位"-"为文件类型(-代表文件:d代表目录:l代表软链接文件即快捷方式),后面每3位一组. -rw-r--r-- rw-   u所有者 ...

  8. 消息中间件kafka+zookeeper集群部署、测试与应用

    业务系统中,通常会遇到这些场景:A系统向B系统主动推送一个处理请求:A系统向B系统发送一个业务处理请求,因为某些原因(断电.宕机..),B业务系统挂机了,A系统发起的请求处理失败:前端应用并发量过大, ...

  9. 【BZOJ3277】串(后缀自动机)

    [BZOJ3277]串(后缀自动机) 题面 BZOJ 题解 广义后缀自动机??? 照着别人的打了一遍.. 相当于每个串都构建一个后缀自动机 构建完一个串之后,直接把当前的last指回root就行了?? ...

  10. SPOJ1825:Free tour II

    题意 luogu的翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Sol 点分治辣 如果是等于\(k\)的话,开个桶取\(max\)就好了 ...