Petrozavodsk Winter Camp, Day 8, 2014, Second Trip
给你一棵树,每次询问一个(a,b),问有多少有路径与a-b没有交集
找lca
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
typedef long long LL;
typedef pair<int, int> P;
const int N = 1e5 + ;
vector<int> g[N];
int dep[N], fa[N][], sz[N]; LL f1[N], f2[N]; int n, q;
LL calc(LL x) {
return x * (x + ) / 2LL;
}
void dfs(int u, int f) {
dep[u] = dep[f] + ;
fa[u][] = f;
sz[u] = ;
for (int v: g[u])
if (v != f) {
dfs(v, u);
sz[u] += sz[v];
}
}
void dfs2(int u, int f) {
for (int v: g[u])
if (v != f) {
dfs2(v, u);
f1[u] += calc(sz[v]);
}
}
void dfs3(int u, int f) {
if (u != ) f2[u] = f2[f] + f1[f] - calc(sz[u]);
for (int v: g[u])
if (v != f) dfs3(v, u);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = ; i >= ; --i)
if (dep[u] - ( << i) >= dep[v]) u = fa[u][i];
if (u == v) return u;
for (int i = ; i >= ; --i)
if (fa[u][i] && fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][];
}
int find(int u, int d) {
dwn(i, , ) if (d >= ( << i)) u = fa[u][d]; return u;
}
LL solve(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int lc = lca(x, y); // dep[x] >= dep[y]
LL ret = ;
if (y == lc) {
ret = f1[x] + f2[x] - f2[y] + calc(n - sz[y]);
}
else {
ret = f1[x] + f1[y] + calc(n - sz[lc]);
ret += f2[x] - f2[lc];
int t1 = find(y, dep[lc] - dep[y] - );
ret -= calc(sz[t1]);
ret += f2[y] - f2[lc];
int t2 = find(x, dep[lc] - dep[x] - );
ret -= calc(sz[t2]);
ret -= f1[lc] - calc(sz[t1]) - calc(sz[t2]);
}
return ret;
}
int main() {
scanf("%d%d", &n, &q);
rep(i, , n - ) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(, );
rep(j, , ) rep(i, , n) fa[i][j] = fa[fa[i][j - ]][j - ];
dfs2(, );
dfs3(, );
// rep(i, 1, n) cout << f2[i] << ' '; cout << '\n';
while (q --) {
int x, y;
scanf("%d%d", &x, &y);
cout << solve(x, y) << '\n';
}
}
/*
6 2
1 2
3 2
3 4
3 5
6 3
5 4
1 6
*/
Petrozavodsk Winter Camp, Day 8, 2014, Second Trip的更多相关文章
- Petrozavodsk Winter Camp, Warsaw U, 2014, A The Carpet
一个地图上有若干障碍,问允许出现一个障碍的最大子矩形为多大? 最大子矩形改编 #include<bits/stdc++.h> using namespace std; #define re ...
- Petrozavodsk Winter Camp, Day 8, 2014, Ship
$dp(i,j)$表示i-j这段还没运走时的状态,包括 运输了多少次,还剩多少空间 每次枚举运输左边还是右边转移 #include <bits/stdc++.h> #define rep( ...
- Petrozavodsk Winter Camp, Day 8, 2014, Fine Brochures
1的个数-块的个数+多减的个数+flag 多减的只会在一个循环末尾出现 #include <bits/stdc++.h> using namespace std; #define rep( ...
- Petrozavodsk Winter Camp, Day 8, 2014, Mosaic
给你三个数字a,b,c,让你用1-m的数字凑出来 结论:有2个1和2个2肯定凑不出来,然后就搜索 #include <bits/stdc++.h> using namespace std; ...
- Petrozavodsk Winter Camp, Day 8, 2014, Rectangle Count
给一个n*m的格点图,问其中有多少个矩形? $ \sum_{x=1}^{nm} \sum_{ab=x} [a + b \leq n](n - a - b + 1)\sum_{cd=x} [c + d ...
- Petrozavodsk Winter Camp, Andrew, 2014, Dichromatic Trees
条件: 1:每个红色节点的儿子都是黑色节点 2.每个叶子到根路径上的黑点数相等,等于某个常数,称作树的black height 求给定black height和节点数的符合条件的方案数 $black_ ...
- Petrozavodsk Winter Camp, Andrew, 2014, Bipartite Bicolored Graphs
由i个点和j个点组成的二分图个数为 $3^{ij}$,减去不联通的部分得到得到由i,j个点组成的联通二分图个数 $g_{i,j} = 3_{ij} - \sum_{k=1}^i \sum_{l=0}^ ...
- 2018 Petrozavodsk Winter Camp, Yandex Cup
A. Ability Draft solved by RDC 60min start, 148 min AC, 1Y 题意:两只 Dota 队伍,每队 \(n\) 个英雄,英雄一开始无技能,他们需要按 ...
- 2019 Petrozavodsk Winter Camp, Yandex Cup C. Diverse Singing 上下界网络流
建图一共建四层 第一层为N个歌手 第二层为{pi,li} 第三层为{si,li} 第四层为M首歌 除了S和第一层与第三层与T之间的边为[1,INF] 其他边均为[0,1] #include<bi ...
随机推荐
- The Non-Inverting Amplifier Output Resistance by Adrian S. Nastase [转载]
Source Address: http://masteringelectronicsdesign.com/the-non-inverting-amplifier-output-resistance/ ...
- package.json中 npm依赖包版本前的符号的意义
版本的格式 major.minor.patch 主版本号.次版本号.修补版本号 ———————————————————— patch:修复bug,兼容老版本 minor:新增功能,兼容老版本 majo ...
- Android配置AndroidAnnotations注解框架配置
1在新建的普通项目里面,新建一个compile-libs文件夹,将下载来的androidannotation-xx.jar添加到compile-libs文件夹下,将androidannotation- ...
- Beautiful Soup 解析html表格
from bs4 import BeautifulSoup import urllib.request doc = urllib.request.urlopen('http://www.bkzy.or ...
- lnmp mysql远程访问设置
一:iptables 设置开放3306访问 iptables -L -n --line-numbers 1,删除DROP 3306 iptables -D INPUT 5[序列号] 2,添加 ACCE ...
- bash 和sed 和gawk
bash内建命令 命令描述 : 扩展参数列表,执行重定向操作 . 读取并执行指定文件中的命令(在当前shell环境中) alias 为指定命令定义一个别名 bg 将作业以后台模式运行 bind 将键盘 ...
- Python-王者荣耀自动刷金币+爬取英雄信息+图片
前提:本文主要功能是 1.用python代刷王者荣耀金币 2.爬取英雄信息 3.爬取王者荣耀图片之类的. (全部免费附加源代码) 思路:第一个功能是在基于去年自动刷跳一跳python代码上面弄的,思路 ...
- uni-app编写小程序
今天项目还没有开始开发,网上看到了uni-app官网介绍可以一套代码,多端使用,感觉很厉害的样子,就做了个小程序页面试试. uni-app 使用 vue语法,小程序api,内嵌mpvue,使我们能够很 ...
- webapi 下载Ftp文件并返回流到浏览器完成文件下载
ResultModel<HttpResponseMessage> resultModel = new ResultModel<HttpResponseMessage>(Resu ...
- 使用tp访问数据库时提示错误: 'PDO' not found, 和not defined constant mysql_attr_init_command 和call an undefined function Think\Template\simpleXml_load_string()函数
第一个问题: PDO not found 是因为 php没有安装pdo扩展, 无法提供给 php 以 数据库访问功能, 所以 报错是在文件: Think/Db.class.php的里面. 解决方法是: ...