codeforces D. Bear and Tree Jumps
2017-03-20 17:24
411 查看
题目链接
自己好弱啊, 比赛的时候感觉有点想法,但是思维一直非常混乱,没有找对正确的节点合并的方法- -学习了一下题解,感觉通过层次将节点分类的做法非常巧妙,自己没有想到= =在此mark一下。
分析:
求总的路径长度是非常简单的,考虑每条边存在于几个点对路径中就可以了。这道题求的是每条路径⌈LK⌉,当然,简单的求总路径就不可行了。但是可以考虑不全每条路径的长度,使得L整除K,并且不改变原来答案。这样,总和也可以求出了。题解巧妙得通过层次来确定两个节点路径长度与K的整数倍之间需要不全的长度,解出答案。
代码:
自己好弱啊, 比赛的时候感觉有点想法,但是思维一直非常混乱,没有找对正确的节点合并的方法- -学习了一下题解,感觉通过层次将节点分类的做法非常巧妙,自己没有想到= =在此mark一下。
分析:
求总的路径长度是非常简单的,考虑每条边存在于几个点对路径中就可以了。这道题求的是每条路径⌈LK⌉,当然,简单的求总路径就不可行了。但是可以考虑不全每条路径的长度,使得L整除K,并且不改变原来答案。这样,总和也可以求出了。题解巧妙得通过层次来确定两个节点路径长度与K的整数倍之间需要不全的长度,解出答案。
代码:
/*****************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define offcin ios::sync_with_stdio(false) #define DEBUG freopen("#.txt", "r", stdin) #define sigma_size 26 #define lson l,m,v<<1 #define rson m+1,r,v<<1|1 #define slch v<<1 #define srch v<<1|1 #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) const int INF = 0x3f3f3f3f; const ll INFF = 1e18; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const ll mod = 1e9+7; const int maxmat = 10; const ull BASE = 133333331; /*****************************************************/ inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } inline ll bits(ll x) { return !x ? x : bits(x - lowbit(x)) + 1; } /*****************************************************/ const int maxn = 2e5 + 5; ll rt[maxn][5], siz[maxn]; std::vector<int> G[maxn]; int N, K; long long ans; void dfs(int u, int fa, int dep) { rt[u][dep % K] = siz[u] = 1; for (int v : G[u]) if (v != fa) { dfs(v, u, dep + 1); for (int i = 0; i < K; i ++) { for (int j = 0; j < K; j ++) { int dis = ((i + j - 2 * dep) % K + K) % K; int cmp = ((K - dis) % K + K) % K; ans += (ll)cmp * rt[u][i] * rt[v][j]; } } for (int i = 0; i < K; i ++) rt[u][i] += rt[v][i]; siz[u] += siz[v]; } ans += (ll)(N - siz[u]) * siz[u]; } int main(int argc, char const *argv[]) { cin>>N>>K; ans = 0; for (int i = 0; i < N - 1; i ++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, -1, 0); cout<<ans / K<<endl; return 0; }
相关文章推荐
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- Code Forces Bear and Forgotten Tree 3 639B
- Code Forces Bear and Forgotten Tree 3 639B
- Codeforces Round #226 (Div. 2) D. Bear and Floodlight
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- 【Codeforces Round】351A - Bear and Game(水)
- Educational Codeforces Round 8 F. Bear and Fair Set(最大流 | Hall定理)★
- codeforces790B - Bear and Tree Jumps
- 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree
- Codeforces C - Bear and String Distance (贪心)
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- Bear and Three Balls CodeForces - 653A
- Codeforces Round #405 A. Bear and Big Brother
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Bear and Friendship Condition CodeForces - 771A(图论,并查集)题解
- Educational Codeforces Round 8 F. Bear and Fair Set(最大流 | Hall定理)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力