hzau1201——Friends(树形DP)
来源:互联网 发布:嵌入式软件 大公司 编辑:程序博客网 时间:2024/06/06 02:40
1201: Friends
Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 129 Solved: 31
[Submit][Status][Web Board]
Description
In a country, the relationship between people can be indicated by a tree. If two people are acquainted with each other, there will be an edge between them. If a person can get in touch with another through no more than five people, we should consider these two people can become friends. Now, give you a tree of N people’s relationship. ( 1 <= N <= 1e5), you should compute the number of who can become friends of each people?
Input
In the first line, there is an integer T, indicates the number of the cases.
For each case, there is an integer N indicates the number of people in the first line.
In the next N-1 lines, there are two integers u and v, indicate the people u and the people
v are acquainted with each other directly. People labels from 1.
Output
For each case, the first line is “Case #k :”, k indicates the case number.
In the next N lines, there is an integer in each line, indicates the number of people who can become the ith person’s friends. i is from 1 to n.
Sample Input
1 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8
Sample Output
Case #1:67777776
【题目大意】
给出一棵树,问每个节点距离六个点以内的点有几个
【题解】
定根维护树形DP,Dw[x][i]数组表示从上往下到达的距离为i的点的个数,
有Dw[x][i]=sum(Dw[son][i-1]),Up[x][i]表示从下往上距离为i的点的个数,
有Up[x][i]=Up[fx][i-1]+Dw[fx][i-1]-Dw[x][i>=2?i-2:0],
两边dfs计算出这两个值,就可以得到每个点的答案了。#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <vector>#include <map>#include <string>#include <stack>using namespace std;typedef long long ll;#define PI 3.1415926535897932#define E 2.718281828459045#define INF 0xffffffff//0x3f3f3f3f#define mod 997const int M=1005;int n,m;int cnt;int sx,sy,sz;int mp[1000][1000];int pa[M*10],rankk[M];int head[M*6],vis[M*100];int dis[M*100];ll prime[M*1000];bool isprime[M*1000];int lowcost[M],closet[M];char st1[5050],st2[5050];int len[M*6];typedef pair<int ,int> ac;//vector<int> g[M*10];ll dp[50][1<<10][50];int has[10500];int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};void getpri(){ ll i; int j; cnt=0; memset(isprime,false,sizeof(isprime)); for(i=2; i<1000000LL; i++) { if(!isprime[i])prime[cnt++]=i; for(j=0; j<cnt&&prime[j]*i<1000000LL; j++) { isprime[i*prime[j]]=1; if(i%prime[j]==0)break; } }}struct node{ int v,w; node(int vv,int ww) { v=vv; w=ww; }};vector<int> g[100005];char str[10005];int ind[2550];int bit[50];int dw[100005][10],up[100005][10];void dfs(int x,int fx){ dw[x][0]=1;//指所有节点自己 for(int i=0; i<g[x].size(); i++) { if(g[x][i]!=fx) { dfs(g[x][i],x);//计算从上到下的DP值,就是先递归到叶子,然后往上推,注意大括号 for(int j=1; j<=6; j++)dw[x][j]+=dw[g[x][i]][j-1]; } }}void dfs2(int x,int fx){ up[x][0]=1; if(x!=fx) //从下往上的dp,递归的每一步是先算DP值,第一次进入dfs2时先算好经过根的那些自下而上的DW { for(int i=1; i<=6; i++) up[x][i]=up[fx][i-1]+dw[fx][i-1]-dw[x][(i-2)>0?(i-2):0];//最后是减掉那些经过x节点而离x的父节点距离是i-1的点,自然,这些点离x的距离自然也是i-2,或者和x同是叶子 } for(int j=0; j<g[x].size(); j++) if(g[x][j]!=fx) dfs2(g[x][j],x);}int main(){ int i,j,k,t; int u,v; scanf("%d",&t); for(i=1; i<=t; i++) { scanf("%d",&n); m=n; for(int p=1;p<=n;p++)g[p].clear(); n--; while(n--) { scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } memset(up,0,sizeof(up)); memset(dw,0,sizeof(dw)); dfs(1,-1);//假设-1是根的父节点 dfs2(1,1);//算自下而上的DP值,第一次需要x==fx处理根的dp printf("Case #%d:\n",i); for(j=1; j<=m; j++) { cnt=0; for(k=1; k<=6; k++) //printf("%d %d\n",up[j][k],dw[j][k]); cnt+=up[j][k]+dw[j][k]; printf("%d\n",cnt); } } return 0;}
- hzau1201——Friends(树形DP)
- HZAU 1201 Friends(树形dp)
- poj2342—树形dp
- HZAU 1201 Friends(树形DP 待整理)
- poj1463——树形DP
- poj4045——树形DP
- hdu3848——树形dp
- 树形DP——POJ3513
- 树形——DP hdu 1520
- 树形DP——POJ1947 and POJ2486
- HDU 2196——Computer(树形DP)
- HDU2196——Computer(树形DP,经典)
- 【POJ2152】Fire——树形DP
- 树的重心——树形dp
- 树形DP——hiho 1055
- 树形DP——Luogu2014 选课
- HDU1520 Anniversary party —— 树形DP
- HDU 2196 Computer——树形dp
- Guava官方文档-RateLimiter类
- 在线编辑器
- 深度学习检测方法梳理
- log4j重写JDBCAppender 解决单引号问题
- The SDK Build Tools revision (23.0.3) is too low for project ':app'. Minimum required is 25.0.0
- hzau1201——Friends(树形DP)
- YOLO: Real-Time Object Detection
- java字符串与二进制的相互转化
- 字符串匹配
- 从零开始学习音视频编程技术(二十) 录屏软件开发之录屏生成MP4
- 深度学习算法之YOLOv2
- No ip domain-lookup和Logging synchronous和Exec-timeout 0 0
- Jmeter 使用集
- Mbean