hzau1201——Friends(树形DP)

来源:互联网 发布:嵌入式软件 大公司 编辑:程序博客网 时间:2024/06/06 02:40

1201: Friends

Time Limit: 1 Sec  Memory Limit: 1280 MB
Submit: 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:6777777

6

【题目大意】

  给出一棵树,问每个节点距离六个点以内的点有几个

 

【题解】

  定根维护树形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;}

1 0
原创粉丝点击