zoukankan      html  css  js  c++  java
  • SPOJ 1435

    算是个经典题目了,很模板的树形DP题目

    做这个题的时候一开始就想到树形DP了,可是由于各种原因没写出来,代码太糟烂了,赛后还是改了好久才过的

    dp(u,0)=sum(dp(v,1));

    dp(u,1)=sum(min(dp(v,0),dp(v,1)));


    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int> G[100010];
    int d[100010][2];
    int x,y,n;
    int dfs(int u,int fa)
    {
        d[u][0]=0;
        d[u][1]=1;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i];
            if(v==fa)continue;
            dfs(v,u);
            d[u][0]+=d[v][1];
            d[u][1]+=min(d[v][0],d[v][1]);
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            G[i].clear();
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d",&x,&y);
            G[x-1].push_back(y-1);
            G[y-1].push_back(x-1);
        }
        dfs(0,-1);
        printf("%d
    ",min(d[0][0],d[0][1]));
        return 0;
    }
    


  • 相关阅读:
    PHP获取指定的时间戳
    Elasticsearch
    git有用基本指令
    php中的json_decode
    有用的sql积累
    git submodule使用原理
    mysql重复插入时insert更改为update更新操作
    jpm
    awk 语句
    tomcat 发布简单的html网站
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3212540.html
Copyright © 2011-2022 走看看