[BZOJ-1369] gem tree DP

Source: Internet
Author: User
1369: [baltic2003] gemtime limit: 2 sec memory limit: 64 MB
Submit: 282 solved: 180
[Submit] [Status] [discuss] Description indicates a tree. You are required to mark the weight value on the node on the tree. The weight can be any positive integer. The only restriction is that the two adjacent nodes cannot mark the same weight value, A solution is required to minimize the total value of the entire tree. Input first gives a number N, representing the tree has n points, n <= 10000 below the N-1 line, representing the two points connected to the output of the minimum total weight sample input10
7 5
1 2
1 7
8 9
4 1
9 7
5 6
10 2
9 3
Sample output14
Hintsourcesolution

Simple tree-like DP

$ DP [I] [J] $ represents the smallest node $ I $ dye color $ J $

Just move it... I thought it was a layer 1 Layer 2 at the beginning, but it seems wrong, it is impossible to get so naive, but I really don't know what the limit is...

PS ask the passing person to teach me how to prove the maximum value of 3...

Code
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int read(){    int x=0,f=1; char ch=getchar();    while (ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}    while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}    return x*f;}#define maxn 10010int N,ans;struct EdgeNode{int next,to;}edge[maxn<<1];int head[maxn],cnt;void add(int u,int v){    cnt++;    edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}void insert(int u,int v) {add(u,v); add(v,u);}int dp[maxn][5];void DFS(int now,int fa){    for (int i=1; i<=3; i++) dp[now][i]=i;    for (int i=head[now]; i; i=edge[i].next)        if (edge[i].to!=fa) DFS(edge[i].to,now);    for (int i=1; i<=3; i++)        for (int j=head[now]; j; j=edge[j].next)            if (edge[j].to!=fa)                {                    int nowc=0;                    for (int k=1; k<=3; k++)                        if (k!=i) nowc=nowc==0?dp[edge[j].to][k]:min(nowc,dp[edge[j].to][k]);                    dp[now][i]+=nowc;                }}int main(){    N=read();    for (int u,v,i=1; i<=N-1; i++) u=read(),v=read(),insert(u,v);    DFS(1,0);    for (int i=1; i<=3; i++) ans=ans==0?dp[1][i]:min(ans,dp[1][i]);    printf("%d\n",ans);    return 0;}

 

[BZOJ-1369] gem 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.