CodeForces 519E 树形DP A and B and Lecture Rooms
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数。
情况还挺多的,少侠不妨去看官方题解。^_^
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ; int n, Q;
vector<int> G[maxn]; int L[maxn];
int fa[maxn];
int sz[maxn]; void dfs(int u)
{
sz[u] = ;
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u]) continue;
fa[v] = u;
L[v] = L[u] + ;
dfs(v);
sz[u] += sz[v];
}
} const int logmaxn = ;
int anc[maxn][logmaxn]; int ancestor(int a, int x)
{
if(!x) return a;
for(int i = ; ( << i) <= x; i++)
if(x & ( << i)) a = anc[a][i]; return a;
} int LCA(int p, int q)
{
if(L[p] < L[q]) swap(p, q);
int log;
for(log = ; ( << log) <= L[p]; log++); log--;
for(int i = log; i >= ; i--)
if(L[p] - ( << i) >= L[q]) p = anc[p][i];
if(p == q) return p;
for(int i = log; i >= ; i--)
if(anc[p][i] && anc[p][i] != anc[q][i])
p = anc[p][i], q = anc[q][i];
return fa[p];
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &n);
for(int i = ; i < n; i++)
{
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
} dfs(); for(int i = ; i <= n; i++) anc[i][] = fa[i];
for(int j = ; ( << j) < n; j++)
for(int i = ; i <= n; i++) if(anc[i][j-])
anc[i][j] = anc[anc[i][j-]][j-]; scanf("%d", &Q);
while(Q--)
{
int u, v; scanf("%d%d", &u, &v); if(u == v) { printf("%d\n", n); continue; } int l = LCA(u, v); if((L[u] + L[v] - L[l] * ) & ) { puts(""); continue; } if(L[u] == L[v])
{
int t = L[u] - L[l];
int uu = ancestor(u, t - ), vv = ancestor(v, t - );
printf("%d\n", n - sz[uu] - sz[vv]);
continue;
} if(L[u] < L[v]) swap(u, v);
int t = L[u] + L[v] - L[l] * ;
int p1 = ancestor(u, t / );
int p2 = ancestor(u, t / - );
printf("%d\n", sz[p1] - sz[p2]);
} return ;
}
代码君
CodeForces 519E 树形DP A and B and Lecture Rooms的更多相关文章
- Codeforces 1153D 树形DP
题意:有一个游戏,规则如下:每个点有一个标号,为max或min, max是指这个点的值是所有子节点中值最大的那一个,min同理.问如何给这颗树的叶子节点赋值,可以让这棵树的根节点值最大. 思路:很明显 ...
- Codeforces 1088E 树形dp+思维
比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...
- Codeforces 1179D 树形DP 斜率优化
题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...
- CodeForces - 337D 树形dp
题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...
- CodeForces 219D 树形DP
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
- Up and Down the Tree CodeForces - 1065F (树形dp)
链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...
- codeforces 1053D 树形DP
题意:给一颗树,1为根节点,有两种节点,min或者max,min节点的值是它的子节点的值中最小的,max节点的值是它的子节点的值中最大的,若共有k个叶子,叶子的值依次为1~k. 问给每个叶子的值赋为几 ...
- Codeforces 1120D (树形DP 或 最小生成树)
题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...
随机推荐
- Mysql系列常见面试题(三)
1.Mysql数据表在什么情况下容易损坏? 服务器突然断电导致数据文件损坏,强制关机,没有先关闭Mysql服务等. 2.Mysql有关全校的表都有那几个? Mysql服务器通过权限表老控制用户对数据库 ...
- jsp使用中$的符号使用失效
解决方法 添加一段话 <%@ page isELIgnored="false"%> 原因:因为jsp servlet版本问题,2.3及2.3之前的版本isELIgno ...
- Java面向对象(static、final、匿名对象、内部类、包、修饰符、代码块)
面向对象 今日内容介绍 u final u static u 匿名对象 u 内部类 u 包的声明与访问 u 四种访问修饰符 u 代码块 第1章 final关键字 1.1 final的概念 继承的出现提 ...
- python中的getcwd
Help on built-in function getcwd in module posix: getcwd(...) getcwd() -> path Return a ...
- Java 反射机制(二)
<Core Java Volume I --- Fundamentals>介绍了三种创建Class对象的方法.本文将举例说明这三种创建Class对象---创建类的方法. 一.public ...
- 重写strcat函数,以实现strcat的功能
char * strcatTest(char *dst,const char *src);Action(){ char a[]="come on"; char b[]=" ...
- python+selenium之验证码的处理
对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码.验证码的类型很多,有字母数字的,有汉字的.甚至还有需要用户输入一道算术题的答案的.对于系统来说,使用验证码可以有效地防止采用机器猜测方 ...
- JAVA-Web05
1 HTTP协议特点 1)客户端->服务端(请求request)有三部份 a)请求行 b)请求头 c)请求的内容,如果没有,就是空白字符 2)服务端->客户端(响应respon ...
- 使Win10用户获得特殊权限以便删除相应文件(夹)
依次访问: 本地用户和组(右击“此电脑”): 用户: 右击:当前用户名: 属性: 添加: 输入:System Managed Accounts Group: 检查名称(可选): 确定: 重启电脑. 参 ...
- BZOJ 4491: 我也不知道题目名字是什么 RMQ
4491: 我也不知道题目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 317 Solved: 174[Submit][Status][ ...