http://poj.org/problem?id=1330

题目意思就是T组树求两点LCA.

这个可以离线DFS(Tarjan)-----具体参考

O(Tn) 0ms

还有其他在线O(Tnlogn)也可参考LCA

// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define IN inline
#define RG register
using namespace std;
typedef long long LL;
const int N=10010;
const int M=10010;
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
int t;int to[M],ne[M];
int fr[N],f[N],d[N],g[N];
IN void link(RG int u,RG int v){
to[++t]=v;ne[t]=fr[u];fr[u]=t;
}
IN int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
IN bool dfs(RG int x){
f[x]=x;d[x]=1;
for(int o=fr[x];o;o=ne[o]){
if(dfs(to[o]))return 1;
f[to[o]]=x;
if(g[to[o]]&&d[g[to[o]]]){
g[to[o]]=g[g[to[o]]]=find(g[to[o]]);
return true;
}
}return 0;
}
int main()
{
freopen("1330.in","r",stdin);
freopen("1330.out","w",stdout);
int T=gi();
while(T--){
int n=gi(),m=n-1;t=0;
memset(fr,0,sizeof(fr));
memset(d,0,sizeof(d));
while(m--){
int u=gi(),v=gi();
n=max(max(u,v),n);d[v]++;
link(u,v);
}int x=gi(),y=gi();
g[x]=y,g[y]=x;
for(int i=1;i<=n;i++)
if(!d[i]){
memset(d,0,sizeof(d));
dfs(i);break;
}
printf("%d\n",g[x]);g[x]=g[y]=0;
}
return 0;
}

  

【Poj 1330】Nearest Common Ancestors的更多相关文章

  1. 【51.64%】【POJ 1330】Nearest Common Ancestors

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...

  2. 【POJ 1330】 Nearest Common Ancestors

    [题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...

  3. POJ 1330:Nearest Common Ancestors

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

  4. 【POJ 1470】 Closest Common Ancestors

    [题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...

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

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

  6. 【LCA/tarjan】POJ1470-Closest Common Ancestors

    [题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...

  7. 【LCA倍增】POJ1330-Nearest Common Ancestors

    [知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...

  8. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  9. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

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

随机推荐

  1. LNMP构架搭建论坛

    1 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel fontconfig fo ...

  2. centos6 磁盘与文件系统管理

    一.磁盘管理 磁盘构成 1.圆形磁盘 2.磁盘读取头 3.机械手臂 4.主轴马达 运作原理 数据存储在具有磁性物质的圆形磁盘上,读写操作主要是通过机械手臂上的磁盘读取头来达成,实际运作时,主轴马达让磁 ...

  3. nginx+redis安装配置(内存型数据库)实现session的共享

    注意:借鉴原文章:http://www.cnblogs.com/roy-blog/p/7196054.html 感兴趣的可以加一下481845043 java交流群,共同进步. 1 session的概 ...

  4. 又一个ajax实例,结合jQuery

    又一个ajax实例,配合jQuery   html <!DOCTYPE html> <html lang="zh-cn"> <head> < ...

  5. Jquery根据JSON生成Table

    先说下背景 本人属于juqery小白中的极品小白.基本对于JS jquery这些不懂.用到时候基本百度下 拿过来改改OK. 上面这东西让我弄了三天.可能对于其他人来说 一天就搞定了 .看来还真得去学一 ...

  6. Linux下汇编语言学习笔记33 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  7. PHP PDO使用

    PHP操作MySQL数据库方式有三种: *1. mysql 最原始的.纯过程化的 如连接: mysql_connect(主机名,账号,密码); 2. mysqli 改进版的.兼容过程化和面向对象化操作 ...

  8. FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

    http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...

  9. Hive之侧视图(Lateral View)

    Lateral View和UDTF类功能函数一起使用,表中的每一行和UDTF函数输出的每一行进行连接,生成一张新的虚拟表,可以对UDTF产生的记录设置字段名称,新加的字段可以使用在sort by,gr ...

  10. GNS3模拟的硬件

    Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...