【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 ...
随机推荐
- centos6 rpm安装mysql(5.5版本)包括 error : Failed dependencies:libaio的解决办法.
1.先在/opt目录下放了两个rpm包 2.先看系统中是否有其他版本的mysql的rpm包 rpm -qa | grep -i mysql 命令结果如下图: 如果没有此步跳过,否则执行一下命令将其删除 ...
- [Python3网络爬虫开发实战] 3.1-使用urllib
在Python 2中,有urllib和urllib2两个库来实现请求的发送.而在Python 3中,已经不存在urllib2这个库了,统一为urllib,其官方文档链接为:https://docs.p ...
- DNS服务器原理简述、搭建主/从DNS服务器并实现智能解析
1. TLD:Top Level Domain 顶级域名 组织域:.com, .net, .org, .gov, .edu, .mil 国家域:.iq, .tw, .hk, .jp, .cn, ... ...
- CURL PHP模拟浏览器get和post
模拟浏览器get和post数据需要经常用到的类, 在这里收藏了几个不错的方法 方法一 <?php define ( 'IS_PROXY', true ); //是否启用代理 /* cookie文 ...
- linux下mysql的安装与使用
一.mysql的安装 之前搭建linux下项目的发布,最后遗留的问题时数据库的迁移,如何从windows上迁移到linux上?这里首先进行mysql数据库的安装 1.下载mysql安装包 在这里下载的 ...
- Oracle 实现查询不区分大小写(设置数据库)
转http://blog.csdn.net/shl7765856/article/details/7622756 查询数据的时候. SQL Server 默认 不区分大小写. 如果要区分,就要额外的设 ...
- table中JS选取行列
<table border="1" align="center" style="width: 500px" id="doct ...
- STM32F407 跑马灯 库函数版 个人笔记
原理图: MCU在开发板原理图的第二页,LED在开发板原理图的第三页 由图可知,PF9 ,PF10 若输出低电平则灯亮,高电平则灯灭 选推挽输出 代码步骤 使能IO口时钟. 调用函数RCC_AHB1P ...
- 九度oj 题目1023:EXCEL排序
题目1023:EXCEL排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20699 解决:4649 题目描述: Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似 ...
- mysql proxy讀寫分流(二)-加入RW splitting
上一篇中提到 安裝LUA及MySQL Proxy後,接下來就是RW splitting(讀寫分流)的部份了 整體的概念圖跟上一篇MySQL Proxy安裝方式相同,丫忠再補上一個對應port的圖表: ...