当前位置:网站首页>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;
}
边栏推荐
- A debugging to understand the slot mechanism of redis cluster
- [deep learning] use deep learning to monitor your girlfriend's wechat chat?
- 辅音和声母的区别?(声母与辅音的区别)
- BlocProvider 为什么感觉和 Provider 很相似?
- The difference between NiO and traditional IO
- Difference and use between require and import
- Pytest collection (2) - pytest operation mode
- plantuml介绍与使用
- Mask wearing detection method based on yolov5
- List announced | outstanding intellectual property service team in China in 2021
猜你喜欢

Flume interview questions

完全注解的ssm框架搭建

"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition

微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练

AirServer2022最新版功能介绍及下载

Little p weekly Vol.11

Significance and measures of security encryption of industrial control equipment

Redis配置与优化

按照功能对Boost库进行分类

locust 系列入门
随机推荐
完全注解的ssm框架搭建
Communication between browser tab pages
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Pytest Collection (2) - mode de fonctionnement pytest
Internet of things RFID, etc
详解LockSupport的使用
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
linux下清理系统缓存并释放内存
【单体】流辰信息I-BPSv3服务器推荐配置
首席信息官对高绩效IT团队定义的探讨和分析
K-means based user portrait clustering model
Application of real estate management based on 3D GIS
物联网rfid等
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
【MySQL】数据库优化方法
Introduction and download of the latest version of airserver2022
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
List announced | outstanding intellectual property service team in China in 2021
辅音和声母的区别?(声母与辅音的区别)