Lowest Common Ancestor(CodeChef TALCA)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Waves___/article/details/70881340

In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defined as the lowest vertex that is ancestor of both that two vertices.

Given a tree of N vertices, you need to answer the question of the form "r u v" which means if the root of the tree is at r then what is LCA of u and v.

 

Input

The first line contains a single integer N. Each line in the next N - 1 lines contains a pair of integer u and v representing a edge between this two vertices.

The next line contains a single integer Q which is the number of the queries. Each line in the next Q lines contains three integers r, u, v representing a query.

 

Output

For each query, write out the answer on a single line.

Constraints

20 points:

  • 1 ≤ N, Q ≤ 100

 

40 points:

  • 1 ≤ N, Q ≤ 105
  • There is less than 10 unique value of r in all queries

 

40 points:

  • 1 ≤ N, Q ≤ 2 × 105

 

Example

Input:
4
1 2
2 3
1 4
2
1 4 2
2 4 2

Output:
1
2

 

Explanation

  • "1 4 2": if 1 is the root, it is parent of both 2 and 4 so LCA of 2 and 4 is 1.
  • "2 4 2": the root of the tree is at 2, according to the definition, LCA of any vertex with 2 is 2.



https://vjudge.net/problem/CodeChef-TALCA

给一棵有n个节点的树,n-1条双向边
先给q个询问,
每个询问格式; root u v
    求以root为根的树,u与v的最近公共祖先
    
DFS+并查集的LCA问题

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 2e5 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;

int par[maxn];
int vis[maxn];	
int n, q, flag;
int root, node1, node2;
vector<int>G[maxn]; 

int find(int a)
{
	if (par[a] == -1) return a;
	return par[a] = find(par[a]);
}
void unite(int a, int b)
{
	int x = find(a);
	int y = find(b);
	par[y] = x;
}
void LCA(int u)
{
	vis[u] = 1;
	for (int i = 0; i < G[u].size(); i++){
		int v = G[u][i];
		if (vis[v]) continue;
		LCA(v);
		unite(u, v);
	}
	if (flag) return; // 防止输出两次 
	if (u == node1 && vis[node2]){
		cout << find(node2) << endl;
		flag = 1;
		return;
	}
	if (u == node2 && vis[node1]){
		cout << find(node1) << endl;
		flag = 1;
		return;
	}
}
int main(void)
{
//	freopen("in.txt", "r", stdin);
	cin >> n;
	int u, v;
	for (int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	cin >> q;
	for (int i = 1; i <= q; i++){
		cin >> root >> node1 >> node2; 
		memset(vis, 0, sizeof(vis));
		memset(par, -1, sizeof(par));
		flag = 0;
		LCA(root);
	}

	return 0;
}




猜你喜欢

转载自blog.csdn.net/Waves___/article/details/70881340