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”三种中任意一种就输 ...
随机推荐
- 【MySQL】从入门到精通8-SQL数据库编程
上期:[MySQL]从入门到精通7-设计多对多数据库 第零章:Mac用户看这里: mac终端写MySQL和windows基本相同,除了配置环境变量和启动有些许不同以外. 先配置环境变量,在终端输入vi ...
- 区块相隔虽一线,俱在支付同冶熔,Vue3.0+Tornado6前后端分离集成Web3.0之Metamask区块链虚拟三方支付功能
最近几年区块链技术的使用外延持续扩展,去中心化的节点认证机制可以大幅度改进传统的支付结算模式的经营效率,降低交易者的成本并提高收益.但不能否认的是,区块链技术也存在着极大的风险,所谓身怀利器,杀心自起 ...
- 表单动态增加div元素提交方法
实现的效果如下: 1 var detail_div = 1; 2 var i=0; 3 function add_div() { 4 var e = document.getElementById(& ...
- kingbaseES R3 集群配置 SSL
案例说明: 本测试是在非生产环境下,在官方没有明确声明支持KingbaseCluster使用ssl的前提下,建议只能在测试环境使用,避免生产环境下直接使用. 数据库版本: TEST=# selec ...
- cnblogs-theme-blogure
cnblogs-theme-blogure 又一个博客园主题 Blogure. 它使用 PetiteVue 和 PicoCSS. 喜欢的话可以帮个点 Star 么? 快速开始 确保博客园有 JS 权限 ...
- 重新安装kuboard后,原先配置的CI/CD命令都没了,需要重新创建
背景介绍 使用如下命令创建的kuboard服务,上一层用nginx设置代理,用域名访问使用的 docker run -d \ --restart=always \ --name=kuboard \ - ...
- 深入探究 K8S ConfigMap 和 Secret
ConfigMap 1.什么是 ConfigMap? ConfigMap 是用来存储配置文件的 Kubernetes 资源对象,配置对象存储在 Etcd 中,配置的形式可以是完整的配置文件.key/v ...
- 使用 Auditbeat 模块监控 shell 命令
使用 Auditbeat 模块监控 shell 命令 Auditbeat Audited 模块可以用来监控所有用户在系统上执行的 shell 命令.在终端用户偶尔才会登录的服务器上,通常需要进行监控. ...
- Opengl ES之四边形绘制
四边形的绘制在Opengl ES是很重要的一项技巧,比如做视频播放器时视频的渲染就需要使用到Opengl ES绘制四边形的相关知识.然而在Opengl ES却没有直接提供 绘制四边形的相关函数,那么如 ...
- bfs与dfs基础
bfs像二叉树的层序遍历 像这个图走bfs就{1, 2, 3, 4, 5, 6, 7, 8}这样走: dfs就{1, 2, 5, 6, 3, 7, 8, 4}. bfs与queue相结合,走到哪就把哪 ...