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. mysql多表合并为一张表

    有人提出要将4张表合并成一张.数据量比较大,有4千万条数据.有很多重复数据,需要对某一列进行去重. 数据量太大的话,可以看我另外一篇:http://www.cnblogs.com/magmell/p/ ...

  2. Linux系统安装Apache

    一,Apache和tomcat的区别与联系 apache是web服务器,web服务器专门处理http请求: tomcat是运行在apache上的应用服务器: apache是普通服务器,本身只支持htm ...

  3. python——进制间的转换

    int(string_num, n)  string_num表示某种进制的字符串,n表示string_num是什么进制数 2.8.16 进制转为10进制:使用int()或者eval() 10 进制转为 ...

  4. IntelliJ IDEA配置本地Tomcat方法---亲测有效

    https://blog.csdn.net/hello_ljl/article/details/79258165

  5. navicat mysql 连接本地 忘记密码 查看密码 操作

    https://jingyan.baidu.com/article/454316ab4e9e65f7a7c03ad1.html

  6. java springMVC 极致验证 非demo版

    最近公司项目需要,做了个极致验证,自己在此做下记录. 先上效果图: 它的官网:http://www.geetest.com/   里面有 身份验证.行为验证,  我这使用的为行为验证. 技术文档:ht ...

  7. Codeforces713D. Animals and Puzzle

    $n<=1000,m<=1000$,$n*m$的01矩阵,给$t<=1000000$个询问,每次问一个矩形中最大的1正方形的边长. 先想想不考虑“一个矩形中”的限制,那记$f(i,j ...

  8. msp430入门编程11

    msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程

  9. poj——1470 Closest Common Ancestors

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

  10. Linux下tomcat的catalina.out屏蔽

    修改catalina.sh ,找到下面的位置: if [ -z "$CATALINA_OUT" ] ; then#CATALINA_OUT="$CATALINA_BASE ...