Luogu p3174 [haoi2009] caterpillar tree DP

Source: Internet
Author: User

Portal

Tree-based DP basic question finding the diameter of the weighted tree with vertices

Then, the point weight is the point degree of each point (son [])...

So it can be simplified.

Maintain the maximum and secondary chains starting from the root according to the normal routine

Change + 1 to + son [x]-1 when saving the DP array.

So DP [x] = Maxx + Maxxx + son [x]-1

(Maxx and Maxxx maintain the longest and secondary chains)

While ans get Max

Time cost: 45 min

Code:

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<queue> 6 #define ms(a,b) memset(a,b,sizeof a) 7 #define rep(i,a,n) for(int i = a;i <= n;i++) 8 #define per(i,n,a) for(int i = n;i >= a;i--) 9 #define inf 214748364710 using namespace std;11 typedef long long ll;12 typedef double D;13 #define eps 1e-814 ll read() {15     ll as = 0,fu = 1;16     char c = getchar();17     while(c < ‘0‘ || c > ‘9‘) {18         if(c == ‘-‘) fu = -1;19         c = getchar();20     }21     while(c >= ‘0‘ && c <= ‘9‘) {22         as = as * 10 + c - ‘0‘;23         c = getchar();24     }25     return as * fu;26 }27 //head28 const int N = 300003;29 int n;30 int head[N],nxt[N<<1],mo[N<<1],cnt;31 int son[N];32 void _add(int x,int y) {33     son[x]++;34     mo[++cnt] = y;35     nxt[cnt] = head[x];36     head[x] = cnt;37 }38 void add(int x,int y) {39     if(x^y)_add(x,y),_add(y,x);40 }41 42 int dp[N],ans;43 void dfs(int x,int f) {44     int maxx = 0,maxxx = 0;45     for(int i = head[x];i;i = nxt[i]) {46         int sn = mo[i];47         if(sn == f) continue;48         dfs(sn,x);49         if(dp[sn] > maxx) maxxx = maxx,maxx = dp[sn];50         else if(dp[sn] > maxxx) maxxx = dp[sn];51         dp[x] = max(dp[x],dp[sn] + son[x] - 1);52     }53     ans = max(ans,maxx + maxxx + son[x] - 1);54 }55 56 int main() {57     n = read(),read();58     rep(i,2,n) add(read(),read());59     rep(i,1,n) dp[i] = 1;60     dfs(1,0),printf("%d\n",ans);61     return 0;62 }
View code

 

Luogu p3174 [haoi2009] caterpillar tree DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.