【题目链接】: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的更多相关文章

  1. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 791C】Bear and Different Names

    [题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...

  4. 【codeforces 791B】Bear and Friendship Condition

    [题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...

  5. 【codeforces 791A】Bear and Big Brother

    [题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...

  6. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...

  7. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【19.05%】【codeforces 680D】Bear and Tower of Cubes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. StringBuilder和String的区别

      使用   StringBuilder 语言 C# String   对象是不可改变的.每次使用   System.String   类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...

  2. 关于python的冒号截取

    https://zhidao.baidu.com/question/877855739656978372.html

  3. Loadrunner--web_find和web_reg_find的用法和区别

    一.web_find()函数 该函数的作用是“在页面中查找相应的内容”,常用参数及含义如下: web_find("web_find", //定义该查找函数的名称 "Rig ...

  4. 【Redis】redis的安装、配置执行及Jedisclient的开发使用

    定义: Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred ...

  5. 全双工 串口 stm32

  6. UVA 11367 - Full Tank? dijkstra+DP

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. AE 获取地图上当前选中的要素

    樱木 原文 AE开发----获取地图上当前选中的要素 Code1 int selCount = axMapControl1.Map.SelectionCount; IEnumFeature pEnum ...

  8. Linux下交叉编译gdb,gdbserver+gdb的使用以及通过gdb调试core文件

    交叉编译gdb和gdbserver 1.下载gdb:下载地址为:http://ftp.gnu.org/gnu/gdb/按照一般的想法,最新版本越好,因此下载7.2这个版本.当然,凡事无绝对.我们以gd ...

  9. jQuery weui Select组件显示指定值

    jQuery weui有个支持单选或者多选的select弹出层,默认他是这样的 第2部分选择什么值,第1部分就显示什么值,一般的场景支持是没问题了,但本次开发碰到了一个问题. 需求描述: 职业名称后面 ...

  10. POJ 1745 Divisibility DP

    POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...