【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 ...
随机推荐
- C#调用第三方ocx控件 (winform /aspx)
C#调用第三方ocx控件 1..net环境在工具箱上点右键,选择自定义工具箱,然后选择你需要的COM或者OCX控件就可以了. 2.在自定义工具箱中加入相应的控件,设置id,在客户端脚本中直接引用它 ...
- c++智能指针使用笔记
1. c++智能指针中,c++的memory文件中,有auto_ptr等各种关于智能指针的东西,shared_ptr,weak_ptr在C++11中已经成为标准. 也看了ogs的智能指针,每次引用起来 ...
- 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks
[链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...
- 动态规划求解序列问题(LIS、JLIS)
1. 最长递增子序列 不要求位置连续:要求大小严格递增(strictly increasing) 穷举法解题 首先以每个数字为单位分割寻找最长递增子序列: int lis(const vector&l ...
- iOS_03_为什么选择ios开发
为什么选择ios开发 为什么要选择移动开发 * 手机将是人类最离不开的设备之一,硬件软件参数也越来越强,应用需求量剧增. * 移动互联(就是将移动通信和互联网二者结合起来)发展迅速,各大公司都对移动互 ...
- (转载)iis7下站点日志默认位置
转自http://www.cnblogs.com/mincyw/p/3425468.html iis7下站点日志默认位置 在iis6时,通过iis管理器的日志配置可以找到站点日志存储的位置. 但是 ...
- 2、在uboot上实现电源管理
tar xjf u-boot-1.1.6.tar.bz2 cd u-boot-1.1.6 patch -p1 < ../u-boot-1.1.6_jz2440.patch make 100ask ...
- UIButton UIBarButtonItem用法
#pragma mark 快速创建一个item - (UIBarButtonItem *)itemWithNormal:(NSString *)normal highlighted:(NSString ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- Java反射学习总结一(基础篇)
Class类是Reflection API中核心的类,他位于Java.lang.Class 列出一些常用的方法. - getName() : 获得类的完整名字 - getFields() : 获得类的 ...