【codeforces 791D】 Bear and Tree Jumps
【题目链接】:http://codeforces.com/contest/791/problem/D
【题意】
你可以从树上的节点一次最多走k条边。
(称为跳一次);
树为无权树;
然后问你任意两点之间的条的次数的和为多少;
【题解】
如果k=1的话;
问题就是求任意两点之间的距离的和了;
这个可以在O(N)的复杂度下搞出来;
即
枚举每一条边;
看这条边左边的点的数目和右边的点的数目分别为多少->num1和num2
num1*num2就是经过这条边的路径个数;
累加所有的边的这个值就是任意两点之间的距离了;
但是对于k>1的情况有点不同;
可以假设上面求的k=1的情况的答案为S
则
k>2的答案应该为
(S+∑f(l,k))/k;
这里f(l,k)指的是某个路径长度为l,然后让他能够被k整除还要加上几.
f(10, 3) = 2, f(11, 3) = 1, f(12, 3) = 0.
这里的∑f(l,k)
也能在O(N)*K^2的复杂度内搞出来;
具体的
设某条路径的长度为k;
然后令这条路径对k求余结果为x
比如
有200条边它们的长度对3求余的结果都是1
则这每条边都要加上2才能被3整除;
即结果加上200*2;
然后对于树上的任意两点之间的距离(x,y);
=dep[x]+dep[y]-2*dep[t];
t是它们的最近公共祖先.
然后O(K^2)枚举这条边两端的端点的深度对k的求余结果i和j
i+j-2*dep[now]就是深度%k的结果为i的节点和深度%k的结果为j的节点的距离了.
然后%k得到dis;
用k减去dis再%k就是长度%k为dis的路径,每条路径需要额外加上的距离了.
按照这个方法搞出∑f(l,k)就好;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 20e4+100;
vector <int> G[N];
int n,k;
LL sum[N],a[N][5+2],ans;
void in()
{
rei(n), rei(k);
rep1(i, 1, n - 1)
{
int x, y;
rei(x), rei(y);
G[x].push_back(y),G[y].push_back(x);
}
}
void dfs(int x, int fa,int dep)
{
a[x][dep%k] = sum[x] = 1;
for (int y : G[x])
{
if (y == fa) continue;
dfs(y, x, dep + 1);
rep1(i,0,k-1)
rep1(j, 0, k-1)
{
int dis = ((i + j) % k - ((dep * 2) % k) + k) % k;
int t = (k - dis + k) % k;
ans += t*a[x][i] * a[y][j];
}
rep1(i, 0, k - 1)
a[x][i] += a[y][i];
sum[x] += sum[y];
ans += (n - sum[y])*sum[y];
}
}
void o()
{
cout << ans / k << endl;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
dfs(1, 0,0);
o();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 791D】 Bear and Tree Jumps的更多相关文章
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 791C】Bear and Different Names
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 【codeforces 791A】Bear and Big Brother
[题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【27.48%】【codeforces 699D】 Fix a Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 【干货】前端开发者最常用的六款IDE
一.Visual Studio Code 下载地址:https://code.visualstudio.com/ 功能介绍: 微软在2015年4月30日Build 开发者大会上正式宣布了 Visual ...
- 51Nod——N1284 2 3 5 7的倍数
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1284 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 ...
- Linux 从core信息中找到TLS信息
背景 我们在查core问题时,有时候须要查看某个TLS变量的值.可是GDB没有提供直接的命令,或者我不知道.这篇文字的目的.就是想办法从core文件里找出某个线程存放TLS变量的内容. 依据 Linu ...
- C# SocketAsyncEventArgs类
Namespace:System.Net.Sockets Assemblies:System.Net.Sockets.dll, System.dll, netstandard.dll (Represe ...
- Surging 微服务框架使用入门
原文:Surging 微服务框架使用入门 前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正. 我对 surging 的看法 我目前所在的公司采用架构就是类似与Sur ...
- 洛谷—— P1018 乘积最大
https://www.luogu.org/problem/show?pid=1018#sub 题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年 ...
- Iceberg使用
Iceberg是Mac下比較好用的pkg生成工具. 在files中选择你想要存放(自己文件的目录),生成pkg后目录就会存储在设置的那个目录下. 点击scripts选择pkg安装各个阶段所要运行脚本路 ...
- screenX, clientX, pageX
screenX:鼠标相对屏幕左上角的水平偏移量. clientX:鼠标相对于浏览器左上角的水平偏移量,会随着滚动条的移动而移动. pageX:鼠标相对浏览器左上角的水平偏移量.不会随着滚动条的移动而移 ...
- Oracle Web链接客户端
TreeSoft数据库管理系统 http://www.treesoft.cn
- 从零开始使用git第一篇:下载安装配置
从零开始使用git 第一篇:下载安装配置 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:git撤销操作.分支操作和 ...