POJ 1330:Nearest Common Ancestors
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 20940 | Accepted: 11000 |
Description
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor
of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node
x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common
ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest
common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
Input
N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers
whose nearest common ancestor is to be computed.
Output
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
并查集。之前在hihoCoder第十二周做过类似的。
代码:
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; int father[10005]; void result(int test1,int test2)
{
int node2=test2;
while(father[test1]!=test1)
{
node2=test2;
while(father[node2]!=node2)
{
if(test1==node2)
{
cout<<test1<<endl;
return;
}
node2=father[node2];
}
test1=father[test1];
}
cout<<test1<<endl;
return;
} int main()
{
int count;
cin>>count; while(count--)
{
int fa,son,node,i;
cin>>node; for(i=1;i<10005;i++)
{
father[i]=i;
} for(i=1;i<=node-1;i++)
{
cin>>fa>>son;
father[son]=fa;
}
int test1,test2;
cin>>test1>>test2; result(test1,test2);
}
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
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...
- 【POJ 1330】 Nearest Common Ancestors
[题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 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
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
随机推荐
- ImageSwitcher和GridView的案例开发
(一)ImageSwitcher之手机相册的滑动查看 首先在布局文件上加一个ImageSwitcher,设置它的宽度和高度为match_parent. 在主程序中:首先设置一个存储照片资源的数组,在设 ...
- 1.requests+正则表达式爬猫眼电影TOP100
import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...
- Kali aircrack-ng wifi密码破解(暴力)
1.kali linux安全渗透 2.工具aircrack-ng 3.ifconfig查看你的网卡信息,wlan0即无线网卡,注意,如果没有发现网卡可能是芯片不能被识别,建议选择网卡芯片为3037或r ...
- 解决请求中的post、get乱码问题以及响应的乱码问题
post: get: response:
- 带你了解后渗透工具Koadic
前言: 在朋友的博客上看到推荐的一款工具Koadic,我接触了以后发现很不错这款工具的强大之处我觉得就是拿到shell后的各种模块功能,我就自己写出来发给大家看看吧. 首先把项目克隆到本地: 项目地址 ...
- Django的templates(模板)
目录 Django的templates(模板) 模板传值 模板过滤器 模板语法之标签 常用标签之for标签 常用标签之if标签 常用标签之with标签 自定义过滤器和标签 模板的继承和导入 模板的导入 ...
- Android适配底部虚拟按键的方法
---恢复内容开始--- 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近项目进行适配的时候发现部分(如华为手机)存在底部虚拟按键的手机会因为虚拟按键的存在导致挡住部分界面,因为需要全屏显示 ...
- MyBatisPlus代码生成 mvc项目
package com.test; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus ...
- 关于c++ 感想
前言 在学校开展了c++的课程,但是不得不说相当乏味. 原因很简单: 1.感觉c++很高级,自己就这智商怎么学的会哦,自己给了自己门槛. 2.c++很难快速的做出一个能够展现的项目,缺乏成就感. 3. ...
- 条形码识别手持终端(PDA)人们每日触碰的科技
时尚达人的你,收快递物流时,毫无疑问在有时会好奇心,派送员腰部取出的那把“扫枪”,轻轻地一扫后,给你打开享有开拆快递物流的开心時刻.老湿机的你,是否会突然发觉,泊车交费时收费员哥哥已不找你许多零钱,只 ...