【树形DP】HDU 3534 Tree

通道

题意:给定n个点的树,问树的直径有多少条

代码:

#include<stdio.h>
#include<string.h>
const int N=500000;
const int inf=0x3fffffff;
int head[N],num,Len[N],ans,node[N],k;
struct edge
{
    int ed,w,next;
}e[N*2];
void addedge(int x,int y,int w)
{
    e[num].ed=y;e[num].w=w;e[num].next=head[x];head[x]=num++;
    e[num].ed=x;e[num].w=w;e[num].next=head[y];head[y]=num++;
}
void dfs(int u,int fa)
{
    int i,v,temp;
    Len[u]=0;//最长边
    node[u]=1;//最长边的个数
    for(i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].ed;
        if(v==fa)continue;
        dfs(v,u);
        temp=Len[v]+e[i].w;
        if(temp+Len[u]>ans)//最长边经过v
        {
            k=node[v]*node[u];
            ans=temp+Len[u];
        }
        else if(temp+Len[u]==ans)
            k+=node[v]*node[u];
        if(Len[u]<temp)//更新最长边
        {
            Len[u]=temp;
            node[u]=node[v];
        }
        else if(Len[u]==temp)//更新最长边的个数
            node[u]+=node[v];
    }
}
int main()
{
    int n,i,x,y,w;
    while(scanf("%d",&n)!=-1)
    {
        memset(head,-1,sizeof(head));
        num=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            addedge(x,y,w);
        }
        ans=-inf;k=0;
        dfs(1,0);
        printf("%d %d\n",ans,k);
    }
    return 0;
}

时间: 2024-10-07 08:19:37

【树形DP】HDU 3534 Tree的相关文章

hdu 3534 Tree(树形DP)

题目链接:hdu 3534 Tree 题意: 给你一棵n个节点,n-1条边的树,每条边有一个长度,现在问你最长的边的长度为多少,有多少条. 题解: 其实这种题不用记录最长和次长,我们开两个数组,len[i],num[i]. 表示以i为根结点出发的最长的长度以及最长的边的条数. 然后我们只需要一个dfs,先用子节点的信息来更新答案,然后在更新当前节点的len和num记录的信息. 这样就不用记录最长和次长. 1 #include<bits/stdc++.h> 2 #define mst(a,b)

fwt优化+树形DP HDU 5909

1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include <bits/stdc++.h> 6 // #include <iostream> 7 // #include <cstdio> 8 // #include <cstdlib> 9 // #include <algorithm> 10 // #inc

树形dp/hdu 1011 Starship Troopers

题意 有n个房子,1号为起点房子.每个房子里会消耗一定的士兵来获取一定的价值.现在有m个士兵,求问可以获得的最大价值 注意:走过的房子不能再走 注意2:若要消灭这个房子的bugs,必须全部消灭 分析 典型的树形dp,01背包,因为每个房子里要么全杀死bugs,要么一个不动,只有取或不取两种状态 设f[i][j]表示以i为根节点,消耗j个士兵所能获得的最大价值 则f[i][j]=max(f[son[i]][k] + f[i][j-k]); 答案为f[1][m] Accepted Code 1 /*

树形DP [HDU 2196] Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3526    Accepted Submission(s): 1788 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

HDU 3534 Tree (经典树形dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 离i最远的距离,dp[i][1]是次远距离.   cnt[i][0]则是最远的点的数量,cnt[i][1]表示次远的数量. up[i]表示以i向上 离i最远的距离.   up_cnt[i]表示向上最远的数量. 写的有点麻烦,调试了2小时... 1 //#pragma comment(linker,

(树形DP) hdu 1520

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5619    Accepted Submission(s): 2580 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

(树形DP) hdu 5148

Cities Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 579    Accepted Submission(s): 179 Problem Description Long long ago,there is a knight called JayYe.He lives in a small country.This countr

树形DP [HDU 1561] The more, The Better

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5506    Accepted Submission(s): 3274 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

(树形DP) hdu 3452

Bonsai Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 783    Accepted Submission(s): 395 Problem Description After being assaulted in the parking lot by Mr. Miyagi following the "All Valley Kar