题目大意:

  给出一个无向无环连通图(树),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的更多相关文章

  1. Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

  2. Codeforces Round #805 (Div. 3)E.Split Into Two Sets

    题目链接:https://codeforces.ml/contest/1702/problem/E 题目大意: 每张牌上面有两个数字,现在有n张牌(n为偶数),问能否将这n张牌分成两堆,使得每堆牌中的 ...

  3. Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)

    因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...

  4. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  5. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  6. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  7. 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 ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. systemd之导致内核 crash

    本文主要讲解linux kernel panic系列其中一种情况: Attempted to kill init! exitcode=0x0000000b 背景:linux kernel 的panic ...

  2. 文心大模型api使用

    文心大模型api使用 首先,我们要获取硅谷社区的连个key 复制两个api备用 获取Access Token 获取access_token示例代码 之后就会输出 作文创作 作文创作:作文创作接口基于文 ...

  3. CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))

    CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...

  4. 做自动化测试选择Python还是Java?

    你好,我是测试蔡坨坨. 今天,我们来聊一聊测试人员想要进阶,想要做自动化测试,甚至测试开发,如何选择编程语言. 前言 自动化测试,这几年行业内的热词,也是测试人员进阶的必备技能,更是软件测试未来发展的 ...

  5. django 通过MQTT连接阿里云

    Django MQTT 连接阿里云 目录 Django MQTT 连接阿里云 目录 一.安装库 1.安装Python对接mqtt协议库,paho-mqtt 二. 设备认证,一机一密型接入 三.问题 1 ...

  6. KingbaseES如何更改现有表的主键

    更改主键 假设您必须更改现有表的主键.您可以通过执行以下语句来实现此目的: ALTER TABLE tablename DROP CONSTRAINT tablename_pkey , ADD PRI ...

  7. 果汁 DI 介绍

    Guice (英音同 'juice[果汁]') 是一个为 JDK8 及以上提供的轻量依赖注入框架. 目录 三级标题 三级标题 四级标题 三级标题 三级标题 /** * Animal */ interf ...

  8. 国内外各大物联网IoT平台鸟瞰和资源导航

    一.国内外物联网平台 国内 百度物接入IoT Hub 阿里云物联网套件 智能硬件开放平台 京东微联 机智云IoT物联网云服务平台及智能硬件自助开发平台 庆科云FogCloud Ablecloud物联网 ...

  9. 记Mybatis动态sql

    目录 记MyBatis动态SQL 1.< SQL >标签 2.< if >标签 3.分支标签 1.第一种:用在查询条件上用choose-when:otherwise可不要 2. ...

  10. JAVA中容器设计的进化史:从白盒到黑盒,再到跻身为设计模式之一的迭代器

    大家好,又见面了. 在我们的项目编码中,不可避免的会用到一些容器类,我们可以直接使用List.Map.Set.Array等类型.当然,为了体现业务层面的含义,我们也会根据实际需要自行封装一些专门的Be ...