【Poj 1330】Nearest Common Ancestors
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的更多相关文章
- 【51.64%】【POJ 1330】Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...
- 【POJ 1330】 Nearest Common Ancestors
[题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...
- POJ 1330:Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20940 Accept ...
- 【POJ 1470】 Closest Common Ancestors
[题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- 【LCA/tarjan】POJ1470-Closest Common Ancestors
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
- 【LCA倍增】POJ1330-Nearest Common Ancestors
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
随机推荐
- mysql常用命令用法
Mysql帮助文档地址:http://dev.mysql.com/doc/ 1.创建数据库: create database database_name; 2.选择数据库: use database_ ...
- ubuntu 安装python 编程环境
1. 安装python sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install pyt ...
- php7 安装swoole扩展
昨天无意中看到一篇关于直播的视频教程 里面讲到了swoole,对于这个东西我相信大家(接近1年phper)都是听过它,但没有真正去用它,当然也是不知道如何使用(me too). 此处总结一下(借鉴了几 ...
- 杭电 2124 Repair the Wall(贪心)
Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...
- 精帖转载(关于stock problem)
Note: this is a repost(重新投寄) of my original post here with updated solutions(解决方案) for this problem ...
- 【02】HTML5与CSS3基础教程(第8版)(全)
[02]HTML5与CSS3基础教程(第8版)(全) 共392页. (魔芋:大体上扫了一遍.没有什么新东西,都是入门的一些基础知识.) 已看完. [美]elizabeth cast ...
- 动态创建div(鼠标放上显示二维码)
最近的微信大行其道.各个网站上都给出的微信验证码,进行手机扫描加入. 怎么创建类似与点击鼠标弹出一个浮动的div显示二维码的这种效果. 1.首先制作好这样的图片,写css样式 <style ty ...
- hdu 5029树链剖分
/* 解:标记区间端点,按深度标记上+下-. 然后用线段树维护求出最小的,再将它映射回来 */ #pragma comment(linker, "/STACK:102400000,10240 ...
- 前端开发:CSS3
CSS介绍: CSS能够使页面具有美观一致的效果,并且能够让内容与格式分离,利于扩展 所以,CSS解决了下面两个问题: 1. 将HTML页面的内容与格式分离: 2. 提高web开发的工作效率. CSS ...
- VIM使用技巧14
经常使用vim的童鞋可能会注意到,实际操作过程中,处于插入模式中是非常少的,更多的是查看和浏览,偶尔修改即可.因此,快速从插入模式退出进入普通模式,就显得非常重要.主要有以下四种方式: 一.在插入模式 ...