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

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

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

O(Tn) 0ms

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

  1. // This file is made by YJinpeng,created by XuYike's black technology automatically.
  2. // Copyright (C) 2016 ChangJun High School, Inc.
  3. // I don't know what this program is.
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cmath>
  12. #define IN inline
  13. #define RG register
  14. using namespace std;
  15. typedef long long LL;
  16. const int N=10010;
  17. const int M=10010;
  18. inline int gi() {
  19. register int w=0,q=0;register char ch=getchar();
  20. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  21. if(ch=='-')q=1,ch=getchar();
  22. while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
  23. return q?-w:w;
  24. }
  25. int t;int to[M],ne[M];
  26. int fr[N],f[N],d[N],g[N];
  27. IN void link(RG int u,RG int v){
  28. to[++t]=v;ne[t]=fr[u];fr[u]=t;
  29. }
  30. IN int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
  31. IN bool dfs(RG int x){
  32. f[x]=x;d[x]=1;
  33. for(int o=fr[x];o;o=ne[o]){
  34. if(dfs(to[o]))return 1;
  35. f[to[o]]=x;
  36. if(g[to[o]]&&d[g[to[o]]]){
  37. g[to[o]]=g[g[to[o]]]=find(g[to[o]]);
  38. return true;
  39. }
  40. }return 0;
  41. }
  42. int main()
  43. {
  44. freopen("1330.in","r",stdin);
  45. freopen("1330.out","w",stdout);
  46. int T=gi();
  47. while(T--){
  48. int n=gi(),m=n-1;t=0;
  49. memset(fr,0,sizeof(fr));
  50. memset(d,0,sizeof(d));
  51. while(m--){
  52. int u=gi(),v=gi();
  53. n=max(max(u,v),n);d[v]++;
  54. link(u,v);
  55. }int x=gi(),y=gi();
  56. g[x]=y,g[y]=x;
  57. for(int i=1;i<=n;i++)
  58. if(!d[i]){
  59. memset(d,0,sizeof(d));
  60. dfs(i);break;
  61. }
  62. printf("%d\n",g[x]);g[x]=g[y]=0;
  63. }
  64. return 0;
  65. }

  

【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. centos6 rpm安装mysql(5.5版本)包括 error : Failed dependencies:libaio的解决办法.

    1.先在/opt目录下放了两个rpm包 2.先看系统中是否有其他版本的mysql的rpm包 rpm -qa | grep -i mysql 命令结果如下图: 如果没有此步跳过,否则执行一下命令将其删除 ...

  2. [Python3网络爬虫开发实战] 3.1-使用urllib

    在Python 2中,有urllib和urllib2两个库来实现请求的发送.而在Python 3中,已经不存在urllib2这个库了,统一为urllib,其官方文档链接为:https://docs.p ...

  3. DNS服务器原理简述、搭建主/从DNS服务器并实现智能解析

    1. TLD:Top Level Domain 顶级域名 组织域:.com, .net, .org, .gov, .edu, .mil 国家域:.iq, .tw, .hk, .jp, .cn, ... ...

  4. CURL PHP模拟浏览器get和post

    模拟浏览器get和post数据需要经常用到的类, 在这里收藏了几个不错的方法 方法一 <?php define ( 'IS_PROXY', true ); //是否启用代理 /* cookie文 ...

  5. linux下mysql的安装与使用

    一.mysql的安装 之前搭建linux下项目的发布,最后遗留的问题时数据库的迁移,如何从windows上迁移到linux上?这里首先进行mysql数据库的安装 1.下载mysql安装包 在这里下载的 ...

  6. Oracle 实现查询不区分大小写(设置数据库)

    转http://blog.csdn.net/shl7765856/article/details/7622756 查询数据的时候. SQL Server 默认 不区分大小写. 如果要区分,就要额外的设 ...

  7. table中JS选取行列

    <table border="1" align="center" style="width: 500px" id="doct ...

  8. STM32F407 跑马灯 库函数版 个人笔记

    原理图: MCU在开发板原理图的第二页,LED在开发板原理图的第三页 由图可知,PF9 ,PF10 若输出低电平则灯亮,高电平则灯灭 选推挽输出 代码步骤 使能IO口时钟. 调用函数RCC_AHB1P ...

  9. 九度oj 题目1023:EXCEL排序

    题目1023:EXCEL排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20699 解决:4649 题目描述:     Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似 ...

  10. mysql proxy讀寫分流(二)-加入RW splitting

    上一篇中提到 安裝LUA及MySQL Proxy後,接下來就是RW splitting(讀寫分流)的部份了 整體的概念圖跟上一篇MySQL Proxy安裝方式相同,丫忠再補上一個對應port的圖表: ...