codeforces 161D 点分治
传送门:https://codeforces.com/problemset/problem/161/D
题意:
求树上点对距离恰好为k的点对个数
题解:
与poj1741相似
把点分治的模板改一下即可,我们依然是求得一个dep数组,然后根据这个dep数组来更新两点间的距离,由于k的范围只有500,所以我们可以直接开一个500的数组来统计两点间距离的数量
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
struct EDGE {
int v, w, nxt;
} edge[maxn << 1];
int head[maxn], tot;
void add_edge(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
int sz[maxn], son[maxn], dep[maxn], vis[maxn];
int Maxt, root, Allnode, cnt;
LL ans;
int n, k;
void get_root(int u, int fa) {
sz[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v] && v != fa) {
get_root(v, u);
sz[u] += sz[v];
}
}
int tmp = max(sz[u] - 1, Allnode - sz[u]);
if(Maxt > tmp) Maxt = tmp, root = u;
}
void dfs(int u, int fa, int len, int dis) {
dep[++cnt] = dis;
if(dis >= len) return;
for(int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v] && v != fa) {
dfs(v, u, len, dis + 1);
}
}
}
LL cal(int rt, int fa, int len) {
if(len <= 0) return len == 0;
cnt = 0;
dfs(rt, fa, len, 0);
LL res = 0;
int num[505]{};
for(int i = 1; i <= cnt; i++) {
num[dep[i]]++;
}
for(int i = 1; i <= cnt; i++) {
res += num[len - dep[i]];
}
return res;
}
void divide(int rt) {
vis[rt] = 1;
// debug1(ans);
ans += cal(rt, 0, k);
for(int i = head[rt]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v]) {
ans -= cal(v, rt, k - 2);
Allnode = sz[v];
Maxt = n;
get_root(v, rt);
divide(root);
}
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
while(scanf("%d%d", &n, &k) != EOF) {
memset(head, -1, sizeof(head));
tot = 0;
for(int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v, 1);
add_edge(v, u, 1);
}
memset(vis, 0, sizeof(vis));
Allnode = n;
Maxt = INF;
get_root(1, 0);
divide(root);
printf("%lld\n", ans/2);
}
return 0;
}
codeforces 161D 点分治的更多相关文章
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 475D CGCDSSQ(分治)
题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...
- Codeforces 161D Distance in Tree
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...
- Codeforces 888G(分治+trie)
按位贪心,以当前考虑位是0还是1将数分成两部分,则MST中这两部分之间只会存在一条边,因为一旦有两条或以上的边,考虑两条边在原图中所成的环,显然这两条边有一条是环上的权值最大边,不会出现在MST中.则 ...
- Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...
随机推荐
- Android开发-API指南-<activity-alias>[原创译文]
http://blog.sina.com.cn/s/blog_48d491300100zmg5.html
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...
- MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数
MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...
- VSCode 设置 CPP 代码风格
VSCode 设置 CPP 代码风格 按 Ctrl+, 打开设置,输入 format 找到. { BasedOnStyle: Google, IndentWidth: 4 }
- GMTC2019|闲鱼-基于Flutter的架构演进与创新
2012年应届毕业加入阿里巴巴,主导了闲鱼基于Flutter的新混合架构,同时推进了Flutter在闲鱼各业务线的落地.未来将持续关注终端技术的演变及趋势 Flutter的优势与挑战 Flutter是 ...
- ORACLE学习笔记-ORACLE(基本命令)
--查看VGA信息: show sga; select * from v$sgastat;--可以通过以下几个动态性能视图查看信息: V$sysstat 系统统计信息 V ...
- top-100-of-the-best-useful-opensource-applications/
top-100-of-the-best-useful-opensource-applications/ http://www.ubuntulinuxhelp.com/top-100-of-the-be ...
- day3_python之函数基础知识
一 .为何要用函数之不用函数的问题 #1.代码的组织结构不清晰,可读性差 #2.遇到重复的功能只能重复编写实现代码,代码冗余 #3.功能需要扩展时,需要找出所有实现该功能的地方修改之,无法统一管理且维 ...
- Mysql错误:#1054 - Unknown column 'id' in 'field list' 解决办法
第一次用mysql,在插入数据时,竟然报这样的错误, #1054 - Unknown column 'id' in 'field list'
- Android 隐藏EditText的焦点
在页面的开发过程中,我们可能会遇到这样的情况,打开某个页面(Activity)时,如果该页面中有EditText组建,则会自动弹出软键盘(因为该EditText自动获取焦点了),这样很容易影响用户体验 ...