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. Android5.1系统WebView内存泄漏场景

    问题现象 (该文章,引自零号路的私人博客,本人在浏览框架的开发过程中,用该方式,规避了内存泄露的问题.) 在Android5.1系统中,会发现App存在 WebView 泄漏情况,还比较严重.并且只是 ...

  2. org.hibernate.exception.GenericJDBCException: Could not open connection

    1.错误描述 org.hibernate.exception.GenericJDBCException: Could not open connection at org.hibernate.exce ...

  3. Error:Error #2174

    1.错误描述 Error:Error #2174 : 对于每个FileReference,每次只能执行一个下载.上载.加载或保存操作 2.错误原因 Flex中,在做单文件上传时,多次点击"上 ...

  4. 关于js中 toFixed()的一个小坑

    作为一名前端,大家都应该知道,toFixed()的作用,toFixed()经常用于前台与后台数据格式的转换,套用下w3c上面的定义: 定义和用法toFixed(n) 方法可把 Number 四舍五入为 ...

  5. Xshell配色为ubuntu风格

    背景 为了远程连接服务器,用Xshell作为连接工具,因为好(mian)用(fei),服务器是ubuntu的,因此看不习惯Xshell自带的黑白色,下面给出了ubuntu的配色方案,使用的时候直接导入 ...

  6. RobotFramework下的http接口自动化Get关键字的使用

    在使用Create Http Context关键字创建了http的调用环境后,就可以使用Get关键字来发送一个http协议中最常用的get请求了. Get 关键字只接受一个url参数,该关键字的API ...

  7. 试着讲清楚:js代码运行机制

    一. js运行机制 js执行引擎 经常看文章的说到js是带线程的,其实这个说法非常的模糊,准确的是js执行引擎是单线程的,js执行引擎就是js代码的执行器,有了这个概念就可以下来说说js是如何运行的了 ...

  8. SAXParser解析xml文件

    对于xml的解析,这里学习并演示使用SAXParser进行解析的样例. 使用此种方法无法解析"gb2312"编码的xml文件,因此,此处xml文件编码设置为"UTF-8& ...

  9. 【BZOJ2160】拉拉队排练(回文树)

    [BZOJ2160]拉拉队排练(回文树) 题面 BZOJ 题解 看着题目, 直接构建回文树 求出每个回文串的出现次数 直接按照长度\(sort\)一下就行了 然后快速幂算一下答案就出来了 这题貌似可以 ...

  10. 【noip模拟】局部最小值

    TimeLimit: 1000ms               MemoryLimit: 256MB Description 有一个n行m列的整数矩阵,其中1到n×m之间的每个整数恰好出现一次.如果一 ...