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 ...
随机推荐
- Java获取服务器系统默认编码格式
大佬教的,做个笔记方法一(推荐):新建一个jsp页面在webapp下然后添加 <% out.print(System.getProperties().getProperty("file ...
- springboot 学习笔记(三)
(三)用jar包启动springboot项目 1.首先需要在pom文件中添加依赖,spring-boot-starter-parent包含有打包的默认配置,如果要修改的话要可以进行重新定义,具体内容参 ...
- 8.html表格相关的标记9.html表格实战《简单的网页布局》
<html> <head> <title>第八课标题表格</title> <meta charset="utf-8"> ...
- java代码关闭tomcat程序
1.通过java代码实现tomcat的关闭 2.tomcatStop.java package test; import java.io.BufferedReader; import java.io. ...
- 利用jieba第三方库对文件进行关键字提取
已经爬取到的斗破苍穹文本以TXT形式存储 代码 import jieba.analyse path = 'C:/Users/Administrator/Desktop/bishe/doupo.text ...
- Dungeon Master的两种方法
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- WPF知识点全攻略11- 命令(Command)
先看一下命令的简单使用: <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Cut ...
- 得到本地应用程序的EXE的路径
实现效果: 知识运用: Application类的ExecutablePath属性 //获取启动了应用程序的可执行文件的路径和可执行文件的名称 public static string Exec ...
- axios的post请求方法---以Vue示例
Axios向后端提交数据的参数格式是json,而并非用的是form传参,post表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,而使 ...
- 用Windows Native API枚举所有句柄及查找文件句柄对应文件名的方法
枚举所有句柄的方法 由于windows并没有给出枚举所有句柄所用到的API,和进程所拥有的句柄相关的只有GetProcessHandleCount这个函数,然而这个函数只能获取到和进程相关的句柄数,不 ...