当前位置:网站首页>Recent public ancestor offline practice (tarjan)
Recent public ancestor offline practice (tarjan)
2022-07-01 22:30:00 【Zqchang】
List of articles
Online and offline
Online and offline are generally for questions that are asked
Online practice : Every time I read a question , You can calculate an answer immediately , Then the output
Offline approach : We must first read all the questions in a unified way , Then after unified calculation , Then output all queries in a unified way
tarjan O(n + m)
n It's the number of nodes ,m Is the number of inquiries
The essence is to optimize the upward marking method
Upward marking method
See an online practice for details
In the depth first traversal process, all points are divided into three categories
1. It's been traversed , And back to the point , Searched , And the subtree was searched
2. Branch being searched
3. Points that have not been searched 
Suppose you search from left to right , It forms such a tree. Green is the first , Red is the second , That is, a path , The others are the third

For the point being searched , Let's look for related questions , You can see from the picture above , Current inquiry point x And the common ancestor of the green part are the general points of the path , Because the subtree at the midpoint of the path can be merged into that point , Finally, when we seek common ancestors , Just look at the point to which that point is merged , It can be realized by combining and searching sets , This is to find the ancestor node
such , Each point will be traversed only once , It will only be merged once , For each query, it will only be queried once
The main idea
When we traverse the current branch , For the common ancestor of all nodes related to this point , We can find out , If this point has been traversed , The common ancestor of the whole subtree is the point in the path , That is, the root node of the subtree , That is, after traversing a point every time , You can merge the current point directly into the root node , When we look up , You can directly check who is the representative element of the current element in the parallel search set , This representative element is its common ancestor
Pay attention to when to merge , That is, after the backtracking , Then merge it into the parent node
Example
distance
The meaning of the question is to ask for the distance between two points 
The method is shown in the figure , Calculate the distance to the root node , Subtract twice the distance of the common ancestor
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define PII pair<int, int>
const int N = 1e4 + 10;
#define x first
#define y second
vector<PII> v[N];
int n, m, a, b, k;
int dist[N];// Store the distance from each point to the root node
int p[N];
int res[N * 2];// Save the answer
int st[N];
vector<PII> query[N]; // first Another query save point ,second Save query number
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
void dfs(int u, int fa)
{
for(auto i : v[u])
{
if(i.x == fa) continue;
dist[i.x] = dist[u] + i.y;
dfs(i.x, u);
}
}
void tarjan(int u)
{
st[u] = 1;
for(auto i : v[u])
{
if(!st[i.x])
{
tarjan(i.x);
p[i.x] = u;
}
}
for(auto i : query[u])
{
if(st[i.x] == 2)// Check whether another point has been traced , If you haven't looked back, jump
{
int anc = find(i.x);
res[i.y] = dist[u] + dist[i.x] - dist[anc] * 2;
}
}
st[u] = 2;
}
signed main()
{
cin >> n >> m;
for(int i=1; i<=n-1; i++)
{
cin >> a >> b >> k;
v[a].push_back({
b, k});
v[b].push_back({
a, k});
}
for(int i=1; i<=m; i++)
{
cin >> a >> b;
if(a != b)
{
query[a].push_back({
b, i});
query[b].push_back({
a, i});
}
}
for(int i=1; i<=n; i++) p[i] = i;
dfs(1, -1);
tarjan(1);
for(int i=1; i<=m; i++) cout << res[i] << endl;
return 0;
}
边栏推荐
- Go — 相关依赖对应的exe
- Talking from mlperf: how to lead the next wave of AI accelerator
- Can I choose to open an account for stock trading on flush? Is it safe?
- 灵动微 MM32 多路ADC-DMA配置
- 手动实现function isInstanceOf(child,Parent)
- MySQL empties table data
- 名单揭晓 | 2021年度中国杰出知识产权服务团队
- Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
- String类型转换BigDecimal、Date类型
- 小 P 周刊 Vol.11
猜你喜欢

名单揭晓 | 2021年度中国杰出知识产权服务团队

“丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中

Copy ‘XXXX‘ to effectively final temp variable

工控设备安全加密的意义和措施

IDA动态调试apk

基于LSTM模型实现新闻分类

Training on the device with MIT | 256Kb memory
![比较版本号[双指针截取自己想要的字串]](/img/19/4f858ffdc1281d6b8b18a996467f10.png)
比较版本号[双指针截取自己想要的字串]

【MySQL】explain的基本使用以及各列的作用

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
随机推荐
详解JMM
【单体】流辰信息I-BPSv3服务器推荐配置
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
Redis配置与优化
The leader of the cloud native theme group of beacon Committee has a long way to go!
Burpsuite simple packet capturing tutorial [easy to understand]
Why does blocprovider feel similar to provider?
使用闭包实现点击按钮切换 toggle
比较版本号[双指针截取自己想要的字串]
Aidl basic use
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Talking from mlperf: how to lead the next wave of AI accelerator
Matlab traverses images, string arrays and other basic operations
物联网rfid等
Getting started with the lockust series
灵动微 MM32 多路ADC-DMA配置
AirServer手机第三方投屏电脑软件
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
【MySQL】数据库优化方法