【题目链接】: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. 【CS Round #46 (Div. 1.5) E】Ultimate Orbs

    [链接]链接 [题意] n个人从左到右站在一条直线上.每个人都有一个能力值g[i],然后每个人可以将相邻的一个人打败. 然后它的能力值能够增加相应的能力值(就是打败了的那个人的能力值). A能够打败B ...

  2. treap-名次树-树堆

    #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; struc ...

  3. amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid

    amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid 一.总结 1.与grid区别:网格中:am-g + am-u-xx-n 等分网格中只有一个: am-avg-sm-4(在u ...

  4. Web自动化测试 Selenium+Eclipse+Junit+TestNG+Python

    Selenium+Eclipse+Junit+TestNG+Python 第三步 下载Selenium IDE.SeleniumRC.IEDriverServer.SeleniumClient Dri ...

  5. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  6. Snail—Hibernate各种异常

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXExNzkxNDIyMDE4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. mysql触发器语法的一个实例

    我要实现的功能是:在更新一个表时.从三个表中查询记录并插入到另外一个表中.以下是我写触发器的过程: 第一次写的触发器例如以下: CREATE TRIGGER istmingxi  AFTER UPDA ...

  8. [TypeStyle] Use fallback values in TypeStyle for better browser support

    You can increase the browser support of your CSS using fallback values and vendor prefixes. This les ...

  9. [RxJS] Conclusion: when to use Subjects

    As a conclusion to this course about RxJS subjects, let's review when and why should you use them. F ...

  10. 【35.02%】【codeforces 734A】Vladik and flights

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