给出一棵树,有若干次询问,每次询问距两个点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的更多相关文章

  1. Codeforces 1153D 树形DP

    题意:有一个游戏,规则如下:每个点有一个标号,为max或min, max是指这个点的值是所有子节点中值最大的那一个,min同理.问如何给这颗树的叶子节点赋值,可以让这棵树的根节点值最大. 思路:很明显 ...

  2. Codeforces 1088E 树形dp+思维

    比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...

  3. Codeforces 1179D 树形DP 斜率优化

    题意:给你一颗树,你可以在树上添加一条边,问添加一条边之后的简单路径最多有多少条?简单路径是指路径中的点只没有重复. 思路:添加一条边之后,树变成了基环树.容易发现,以基环上的点为根的子树的点中的简单 ...

  4. CodeForces - 337D 树形dp

    题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...

  5. CodeForces 219D 树形DP

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  6. codeforces 337D 树形DP Book of Evil

    原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...

  7. Up and Down the Tree CodeForces - 1065F (树形dp)

    链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...

  8. codeforces 1053D 树形DP

    题意:给一颗树,1为根节点,有两种节点,min或者max,min节点的值是它的子节点的值中最小的,max节点的值是它的子节点的值中最大的,若共有k个叶子,叶子的值依次为1~k. 问给每个叶子的值赋为几 ...

  9. Codeforces 1120D (树形DP 或 最小生成树)

    题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...

随机推荐

  1. A. Arya and Bran

    A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. dao层写展示自己需要注意的问题

    写dao层时一定要把 News news=new News(); 写在while循环内,是每循环一次,new出一个对象

  3. 用Meta标签代码让360双核浏览器默认极速模式打开网站不是兼容模式

    公司所作的页面在360下打开都会遇到在360下自动跳到360兼容模式引发许多兼容问题,摸索了好久终于在网上找到了怎么解决的方法,详情如下: 其实360给网站开发者设计了一种选择的方法,只要加入一段Me ...

  4. cssText在js中写样式表兼容全部

    oDiv.style.cssText="width:100px;height:200px;";是前面的升级版(oDiv.style.width='200px';) <styl ...

  5. Python之查询最新的文件

    import os # 定义文件的目录 result_dir = r'E:\python\测试报告' lists = os.listdir(result_dir) # 重新按时间对目录下的文件进行排序 ...

  6. mysql-新增、更新、删除语句

    1.插入数据: INSERT INTO t_book VALUES(NULL,'我爱我家',20,'张三',1); INSERT INTO t_book(id,bookName,price,autho ...

  7. LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例

    题目描述 一共有 nnn个数,第 iii 个数 xix_ix​i​​ 可以取 [ai,bi][a_i , b_i][a​i​​,b​i​​] 中任意值.设 S=∑xi2S = \sum{{x_i}^2 ...

  8. MovieReview—Black Panther(黑豹)

    Justice & Evil   The night before the night, i saw the latest movie in the Marvel series at JiaH ...

  9. 程序员的智囊库系列之3--分布式文件系统(Distributed file systems)

    程序员的智囊库系列之3--分布式文件系统(Distributed file systems) 这是程序员的智囊库系列的第三篇文章.上一篇文章本来打算介绍几个搭建网站的框架,但由于这部分的内容较多,还需 ...

  10. 日常-acm-子序列的和

    输入两个正整数n<m<10^6,输出,保留五位小数.输入包含多组数据,结束标记为n=m=0. 样例输入: 2 4 65536 655360 0 0 样例输出: Case 1:0.42361 ...