A and B and Lecture Rooms
A and B and Lecture Rooms
题意要求我们找有多少个点iii满足dis(i,x),dis(i,y)dis(i, x), dis(i, y)dis(i,x),dis(i,y),输出点iii的数量即可。
首先特判无解的情况就是dis(x,y)dis(x, y)dis(x,y)为奇数时,接下来我们讨论有解的情况,大致分为两类。
首先我们一定可以在x−>yx->yx−>y的路径上找到一个点满足要求。
/*Author : lifehappy
*/
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int head[N], to[N], nex[N], cnt = 1;int fa[N], top[N], son[N], sz[N], dep[N], id[N], rk[N], tot;int n, m;void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void dfs1(int rt, int f) {fa[rt] = f, dep[rt] = dep[f] + 1;sz[rt] = 1;for(int i = head[rt]; i; i = nex[i]) {if(to[i] == f) continue;dfs1(to[i], rt);sz[rt] += sz[to[i]];if(!son[rt] || sz[son[rt]] < sz[to[i]]) son[rt] = to[i];}
}void dfs2(int rt, int tp) {top[rt] = tp;rk[++tot] = rt;id[rt] = tot;if(!son[rt]) return ;dfs2(son[rt], tp);for(int i = head[rt]; i; i = nex[i]) {if(to[i] == fa[rt] || to[i] == son[rt]) continue;dfs2(to[i], to[i]);}
}int lca(int x, int y) {while(top[x] != top[y]) {if(dep[top[x]] < dep[top[y]]) swap(x, y);x = fa[top[x]];}return dep[x] < dep[y] ? x : y;
}int dis(int x, int y) {return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}int get_fa(int x, int k) {while(k > id[x] - id[top[x]]) {k -= id[x] - id[top[x]] + 1;x = fa[top[x]];}return rk[id[x] - k];
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d", &n);for(int i = 1; i < n; i++) {int x, y;scanf("%d %d", &x, &y);add(x, y);add(y, x);}dfs1(1, 0);dfs2(1, 1);scanf("%d", &m);for(int i = 1; i <= m; i++) {int x, y;scanf("%d %d", &x, &y);if(x == y) {printf("%d\n", n);continue;}int d = dis(x, y), l = lca(x, y);if(d & 1) {puts("0");continue;}if(dep[x] < dep[y]) swap(x, y);int p = get_fa(x, d / 2);if(p == l) {int u = get_fa(x, d / 2 - 1), v = get_fa(y, d / 2 - 1);printf("%d\n", n - sz[u] - sz[v]);}else {int u = get_fa(x, d / 2 - 1);printf("%d\n", sz[p] - sz[u]);}}return 0;
}
A and B and Lecture Rooms相关推荐
- A and B and Lecture Rooms CodeForces - 519E LCA+dfs序
看到这个题的第一个思路就是就是统计以每一个点为根的所有节点个数,然后具体就分情况讨论一下即可. 因为刚刚学习了dfs序,这个题就用了dfs序来通过进出时间戳来相减表示其为根的子节点个数. 分情况 我们 ...
- Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language 1.2.2. 基本 ...
- LeetCode 252. Meeting Rooms
原题链接在这里:https://leetcode.com/problems/meeting-rooms/ 题目: Given an array of meeting time intervals co ...
- 3P5 Industrial Engineering Lecture 1-2: Method of Study
3P5 Industrial Engineering Lecture 1-2: Method of Study 文章目录 3P5 Industrial Engineering Lecture 1-2: ...
- lecture 3 qkine- start with connections
lecture 3 qkine start with connections keep an eye on the smart ones for you to recruit in the futur ...
- Colorful Lecture Note(栈的模拟)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lecture note for Little Ho. ...
- Lecture Halls
Lecture Halls (会议安排) 时间限制(普通/Java):1000MS/10000MS 运行内存限制:65536KByte 总提交: 38 测试通过: 2 ...
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机...
Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...
最新文章
- C#中将结构类型数据存储到二进制文件中方法
- 百度地图之根据地图上的点确定地图的放缩比例
- vue 音乐盒app_VBox 一款基于vue开发的音乐盒 序章
- .NET开源社区存在的问题
- 初学Kotlin——在自定义View里的应用
- 做一款仿映客的直播App
- C++堆、栈、自由存储区、全局/静态存储区和常量存储区
- 德标螺纹规格对照表_国标德标对照表
- uni-app开发和常规Vue开发
- 电力用高压锅炉管物理性能检验
- Linux上 如何查找yum安装包所缺缺少的依赖包及报错处理
- TypeScript 安装及基础运行环境搭建 -- 原文来自博客园用户[长岛冰茶。](https://www.cnblogs.com/gaoyd/p/13529026.html)【未修改完成】】...
- 在线观看视频--使用代码倍速播放
- ssm基于javaweb的医疗健康知识管理系统设计与实现毕业设计源码
- php waf 搭建,如何构建属于自己的nginx waf防火墙 VeryNginx
- 6大Word编辑高级技巧,制作文档又快又好看
- 51单片机rtx51-Tiny嵌入式实时操作系统初试
- php如修改登陆后连接地址,两种wordpress更换后台登录界面logo图标方法
- Myeclipse6.0的注册码
- keystore生成证书实例
热门文章
- mysql 一对多 关联一条最新的数据_不得不会的mysql锁
- 我要3万取款机怎么取_7万的新宝骏RS-3怎么样?用车三个月后,车主说出了实话...
- 飞信linux下载文件,OpenFetion(飞信for Linux)
- 从网恋到失恋只需要一秒...
- Python为什么是编程语言中最skr的?
- python find函数_Python 速学!不懂怎么入门python的小白看这篇就够了!
- python 字符编码处理_浅析Python 字符编码与文件处理
- kafka 怎么样连接图形化界面_图形化编程有多简单,点亮LED不到一分钟
- java多线程 sycophantic_Java多线程volatile和synchronized总结
- 初中数学知识点总结_初中数学知识点总结大全_经典版_