给出一棵树,有若干次询问,每次询问距两个点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. Sql server 查询指定时间区间工作日数、休息日数等日期操作

    1.查询指定时间区间的工作日 这个主要难点是法定节假日,国家的法定节假日每年都不一样,还涉及到调休,所以我们设计一个假日表.主要字段有年份,类型(是否调休),假期日期.如下: CREATE TABLE ...

  2. LaTeX小技巧——File ended while scanning use of \@writefile错误的

    早上在修改编译论文时发现了这个问题,仔细检查代码并没发现错误,一时也找不到具体的解决办法.我一直以为是因为runaway argument的错误提示,可实际上就是因为aux文件没有完整输入,导致上次编 ...

  3. Android获取res目录下图片的uri

    Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" ...

  4. 解决ajax 遇到session失效后自动跳转的问题

    在项目中,经常会遇到session失效后,点击任何链接无反应的情况!这样给客户的体验就不是很好,以为是系统出了故障!所以在项目中我们会处理session失效后的跳转问题(一般给用户提示,并跳转后登录页 ...

  5. Java中protected方法访问权限的问题

    先看Test.java 此时出现上文提到的错误:The method clone from the type Object is not visiuable. 我们已经清楚Object.clone() ...

  6. (转载)资源字典(Pro WPF 学习)

    原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html 1.创建资源字典 下面是一个资源字典(AppBrushes.xaml), ...

  7. LibreOJ #2037. 「SHOI2015」脑洞治疗仪

    线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...

  8. 通过WMIC导出系统日志

    查看日志类型 wmic nteventlog get filename C:\>wmic nteventlog get filename FileName appevent secevent s ...

  9. Codeforces Round #313 (Div. 2) A.B,C,D,E Currency System in Geraldion Gerald is into Art Gerald's Hexagon Equivalent Strings

    A题,超级大水题,根据有没有1输出-1和1就行了.我沙茶,把%d写成了%n. B题,也水,两个矩形的长和宽分别加一下,剩下的两个取大的那个,看看是否框得下. C题,其实也很简单,题目保证了小三角形是正 ...

  10. python_99_面向对象多态

    #多态:一种接口,多种实现.主要作用:实现接口重用 #方法1: class Animal(object): def __init__(self,name): self.name=name class ...