【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 ...
随机推荐
- 使用ajax出现canceled情况
在使用ajax的时候要注意,在只定义了一个ajax请求对象的全局变量时,如果同打开发送了一个请求,但在请求还未结束时,又利用这一个全局变量发送另外一个ajax请求,就会出现某一个请求的状态码为canc ...
- CentOS7安装Tomcat9并设置开机启动
1.下载 Tomcat 9 CentOS 7 下创建目录并下载文件: cd /usr/local/ mkdir tomcat cd tomcat wget http://mirrors.hust.ed ...
- leetcode-88合并两个有序数组
合并两个有序数组 思路:利用索引合并两个列表,排序.注意不需要返回值,只修改nums1 class Solution: def merge(self, nums1: List[int], m: int ...
- build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing
maven test项目时遇到一下错误 Some problems were encountered while building the effective model for cn.temptat ...
- 杭电 2111 Saving HDU (贪心)
Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...
- STM32F407 GPIO原理 个人笔记
datasheet(STM32F407ZGT6.pdf)中,IO structure 为FT,表示容忍5V电压 后面的uart1_TX之类,表示端口复用 共有A~G7组IO口, 每组16个IO口:0~ ...
- 59. Spring Boot Validator校验【从零开始学Spring Boot】
大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...
- HDU-2817,同余定理+快速幂取模,水过~
A sequence of numbers Time Limit: 2000/1 ...
- 7-16 一元多项式求导(20 分)(有关while(scanf("%d",&n)!=EOF))
7-16 一元多项式求导(20 分) 设计函数求一元多项式的导数. 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式: 以与输入相同 ...
- mongodb & macOS
mongodb & macOS https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ https://stacko ...