Codeforces Round #805 (Div. 3)G2. Passable Paths
题目大意:
给出一个无向无环连通图(树),n个点n-1条边,m次查询,每次询问给出一个集合,问集合里的树是否都在同一条链上(即能否不重复的走一条边而遍历整个点集)
思路:通过求lca,若有三个点x,y,z
如果满足dix(x,y)+dix(y,z) == dix(x,z),说明此时y位于x,z之间,此时他们就在一条链上,只需要枚举除x,y以外剩下的n-2个点
判断是否满足类似的条件,每次不断跟新x,y的值使其成为一条链上的两个端点。
1 # include<iostream>
2 # include<bits/stdc++.h>
3 using namespace std;
4 const int N = 2e5 + 10;
5 int h[N], e[2 * N], ne[2 * N], idx;
6 int depth[N], fa[N][25];
7 int dis[N];
8 void bfs(int root)/*预处理depth和fa*/
{
9 memset(depth, 0x3f, sizeof depth);
10 queue<int> q;
11 q.push(root);
12 depth[0] = 0,depth[root] = 1;
13 while (q.size()) {
14 int t = q.front();
15 q.pop();
16 for (int i = h[t]; i != -1; i = ne[i]) {
17 int j = e[i];
18 if (depth[j] > depth[t] + 1) {
19 depth[j] = depth[t] + 1;
20 dis[j] = dis[t] + 1;
21 q.push(j);
22 fa[j][0] = t;
23 for (int k = 1; k <= 20; ++k) {
24 fa[j][k] = fa[fa[j][k - 1]][k - 1];
25 }
26 }
27 }
28 }
29 }
30
31 int lca(int a, int b) {/*倍增求lca(最近公共祖先)*/
32 if (depth[a] < depth[b]) swap(a, b);
33 for (int i = 20; i >= 0; --i) {
34 if (depth[fa[a][i]] >= depth[b]) a = fa[a][i];
35 }
36 if (a == b) return a;
37 for (int i = 20; i >= 0; --i) {
38 if (fa[a][i] != fa[b][i]) {
39 a = fa[a][i];
40 b = fa[b][i];
41 }
42 }
43 return fa[a][0];
44 }
45
46 void add(int a, int b) {
47 e[idx] = b;
48 ne[idx] = h[a];
49 h[a] = idx++;
50 }
51 int getdis(int x, int y) {/*两点之间最短距离*/
52 return dis[x] + dis[y] - 2 * dis[lca(x, y)];
53 }
54
55 void solve() {
56 int n;
57 bool ok = 1;
58 cin >> n;
59 if (n == 1 || n == 2) {
60 for (int i = 1; i <= n; ++i) {
61 int x;
62 cin >> x;
63 }
64 cout << "YES" << endl;
65 return;
66 }
67 int x, y;
68 cin >> x >> y;
69 for (int i = 3; i <= n; ++i) {
70 int z;
71 cin >> z;
72 if (getdis(x, z) + getdis(z, y) == getdis(x, y)) continue;
73 if (getdis(x, y) + getdis(y, z) == getdis(x, z)) {
74 y = z;
75 continue;
76 }
77 if (getdis(y, x) + getdis(x, z) == getdis(y, z)) {
78 x = z;
79 continue;
80 }
81 ok = false;
82 }
83 if (ok) cout << "YES" << endl;
84 else cout << "NO" << endl;
85 }
86 int tt;
87
88 int main() {
89
90 int n;
91 cin >> n;
92 memset(h, -1, sizeof h);
93 for (int i = 1; i < n; ++i) {
94 int a, b;
95 cin >> a >> b;
96 add(a, b), add(b, a);
97 }
98 bfs(1);
99 // for(int i = h[1];i!=-1;i = ne[i]) cout<<e[i]<<" ";
100 cin>>tt;
101 while(tt--)solve();
102 return 0;
103 }
Codeforces Round #805 (Div. 3)G2. Passable Paths的更多相关文章
- Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...
- Codeforces Round #805 (Div. 3)E.Split Into Two Sets
题目链接:https://codeforces.ml/contest/1702/problem/E 题目大意: 每张牌上面有两个数字,现在有n张牌(n为偶数),问能否将这n张牌分成两堆,使得每堆牌中的 ...
- Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)
因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #580 (Div. 1)
Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- Linux-shell笔记1
一次执行很多命令,可以用:分割每个命令,依次运行所有命令.但是不是进程列表,要用()包围命令才是进程列表.它们有什么差别呢?进程列表是启动了一个子SHELL来执行的.用echo $BASH_SUBSH ...
- C#基础_理解类
构造函数主要是用来创建对象时为对象赋初值来初始化对象.总与new运算符一起使用在创建对象的语句中 .A a=new A(); 构造函数具有和类一样的名称:但它是一个函数具有函数的所有特性,同一个类里面 ...
- Math_Music
查看代码 #REmoo的优化任务 #1.公式写在<formula_set>类中,统一管理 --- Finished 2022.8.15 12:39 #2.建立<sample_set& ...
- uniapp小程序新版授权登录
1.授权按钮: <view> <button class='login-btn' type='primary' @click="bindGetUserInfo"& ...
- flutter系列之:UI layout简介
目录 简介 flutter中layout的分类 常用layout举例 总结 简介 对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了.布局的英文名叫做layout,就是用来 ...
- KingbaseES sys_prewarm 扩展
Oracle 在查询数据 可以通过cache hint 所访问的数据cache 到数据库buffer,对于KingbaseES,如何将数据加载到cache 了?sys_prewarm 扩展插件可以实现 ...
- 【android 逆向】arm if
#include <stdio.h> void if1(int n){ //if else语句 if(n < 10){ printf("the number less th ...
- MySQL常用函数整理,建议收藏!
常见函数 字符串函数 数字函数 日期函数 聚合函数 流程控制函数 一.字符串函数 concat(s1,s2...,sn) --将s1,s2...,sn连接成字符串,如果该函数中的任何参数为 null, ...
- k8s日志架构和基本日志
如果一个容器崩溃了.一个Pod被驱逐了.或者一个节点停机了,您通常仍然需要访问您应用程序的日志.为此,您需要一个生命周期与节点.Pod.容器相对独立的存储空间来存储应用程序日志和系统日志. 此时,我们 ...
- 使用traefik进行金丝雀发布
文章转载自:https://mp.weixin.qq.com/s/nMMN7hAJK6SFn1V1YyxvHA 在 Traefik 2.0 中以服务负载均衡的形式进行了支持.可以将服务负载均衡器看成负 ...