CF Round #620 E [1-Trees and Queries]

CF Round #620 E 1-Trees and Queries


题意:给你一颗树,进行​次查询,每次查询时在树上加一条边,然后问此时两点间的距离能否为一个特定的值​,(能反复经过同一个节点或同一条边)。

基本思路:加了一条边连接了x,y​以后,a,b两点间的距离就有三种形式,即 ;然后由于能反复经过同一个节点或同一条边,所以答案一定是上面三种可能 + (k为任意自然数),所以直接循环判断就行,我这里是奇偶形式判断(不如直接循环)。 主要知识上的难点为通过lca快速求树上两点间的距离,可以直接套模板。

<pre mdtype="fences" cid="n10" lang="cpp" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include<bits/stdc++.h>
using namespace std;

define ll long long

const int maxn = 3e5+10;
int n, q;
vector<int> G[maxn];
int fa[maxn][21], depth[maxn];


void dfs(int u, int f){
fa[u][0] = f;
for (int i = 1; i <= 20; i++){
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(f != v){
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
}
int lca(int x, int y){
int u = x, v = y;
if(depth[u] < depth[v])
swap(u, v);
for (int i = 20; i >= 0; i--){
if(depth[fa[u][i]] >= depth[v]){
u = fa[u][i];
}
}
if(u == v) return abs(depth[x] - depth[y]);
for (int i = 20; i >= 0; i--){
//if(depth[fa[u][i]] != depth[fa[v][i]]){ //为啥我也不知道...
if(fa[u][i] != fa[v][i]){
u = fa[u][i];
v = fa[v][i];
}
}
return depth[x] + depth[y] - 2 * depth[fa[u][0]];
}
bool check(int x, int y){
return (y>=x) && (x & 1) == (y & 1);
}
int main(){
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
depth[0] = 0;
dfs(1, 0);
cin >> q;
while(q--){
int x, y, a, b, k;
cin >> x >> y >> a >> b >> k;
if(check(lca(a, b), k) || check(lca(a, x)+lca(y, b) + 1, k) || check(lca(a, y) + lca(x, b) + 1, k)){
cout << "YES" << endl;
}
else
cout << "NO" << endl;
}
return 0;
}</pre>


参考:

  1. [RMQ算法]

  2. [最近公共祖先:LCA及其用倍增实现]

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,452评论 0 2
  • 不支持上传文件,所以就复制过来了。作者信息什么的都没删。对前端基本属于一窍不通,所以没有任何修改,反正用着没问题就...
    全栈在路上阅读 2,018评论 0 2
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • (开经偈) 无上甚深微妙法 百千万劫难遭遇 我今见闻得受持 愿解如来真实义 第一品 Fǎ huì yīn yóu ...
    黄一轩阅读 4,393评论 0 1
  • 2019.4.8, 又是一周的开始。 中午接了孩子回来就开始做饭,她午睡习惯了,怕她困,想着让她能早点吃完饭可以睡...
    董依晴妈妈阅读 138评论 0 0