Codeforces Round #405 D. Bear and Tree Jumps 树形DP
来源:互联网 发布:it专业大学排名 编辑:程序博客网 时间:2024/06/09 12:55
传送门
题意:
你可以从树上的节点一次最多走k条边。
(称为跳一次);
树为无权树;
然后问你任意两点之间的条的次数的和为多少;
做法:
枚举边,看这条边左边的点的数目和右边的点的数目分别为多少->num1和num2
num1*num2就是经过这条边的路径个数;
累加所有的边的这个值就是任意两点之间的距离了;
dp[i][j]表示离i这个点,距离%k为j的结点数。
很自然得到ceil(L/k)= L/k+sigma_(i=0)-(i=k-1)/k;
因为枚举的是x-G[x][i]这条边,所以每次处理的时候,最后dp[x][j]+=dp[i][j]。
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)#define S_1(x) scan_d(x)#define S_2(x,y) scan_d(x),scan_d(y)#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_backtypedef long long LL;typedef pair <int, int> ii;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=4e5+10;const int maxx=4e5+10;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;LL dp[maxn][6],a[maxn];int n,k;vector<int>G[maxn];LL ans=0;void dfs(int x,int fa,int deep){ a[x]=1; dp[x][deep%k]=1;// for(int i:G[x]) { if(i==fa) continue; dfs(i,x,deep+1); for(int j=0;j<k;j++) { for(int z=0;z<k;z++) { int len=(j+z-2*deep)%k; int rev=(k-len)%k; ans+=rev*dp[x][j]*dp[i][z]; } } a[x]+=a[i]; for(int j=0;j<k;j++) dp[x][j]+=dp[i][j];// ans+=(n-a[i])*a[i];// }}void solve(){ W(s_2(n,k)!=EOF) { FOr(1,n,i) { int u,v; s_2(u,v); G[u].pb(v); G[v].pb(u); } dfs(1,-1,0); print(ans/k); }}int main(){ //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int t=1; //s_1(t); for(int cas=1;cas<=t;cas++) { //printf("Case %d:\n",cas); solve(); }}
阅读全文
0 0
- Codeforces Round #405 D. Bear and Tree Jumps 树形DP
- codeforces 405 D. Bear and Tree Jumps 树形dp
- Codeforces 791D Bear And Tree Jumps 树形DP
- CF Bear and Tree Jumps树形DP
- Codeforces790B Bear and Tree Jumps -- 树形DP
- codeforces D. Bear and Tree Jumps
- codeforces 791D Bear and Tree Jumps
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- 791D Bear and Tree Jumps
- Codeforces790B. Bear and Tree Jumps 【树型DP】
- CF771C:Bear and Tree Jumps(树形dp & 树上距离和)
- codeforces 711C. Bear and Tree Jumps(树上距离)
- CF791D-Bear and Tree Jumps
- Codeforces Round #318 574D Bear and Blocks(dp)
- Codeforces Round #318 D. Bear and Blocks DP
- Codeforces Round #275 (Div. 1) D Random Function and Tree 树形dp
- Codeforces Round #318-(D. Bear and Blocks)
- Java 课堂笔记 4
- 关于Icon,Image,ImageIcon的简单的对比参考
- 2017-10-7离线赛题解
- SDUT_2449_数据结构实验之栈与队列十:走迷宫
- 工作流创建的三种方式
- Codeforces Round #405 D. Bear and Tree Jumps 树形DP
- 【kmp算法next数组应用】Simpsons’ Hidden Talents HDU
- SpringBoot整合druid数据库连接池
- mysql
- 用construct2做简单游戏
- Spring5源码解析-Spring中的Context loader
- 多态的理解
- 2017暑假训练国庆小假期总结
- 2017年10月8日训练总结