栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1848 Tree

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1848 Tree

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int inf=1000,N=105;int dp[N][3],head[N],nc;bool vis[N];struct Edge{    int to,next;} edge[N*4];void add(int a,int b){    edge[nc].to=b;    edge[nc].next=head[a];    head[a]=nc++;    edge[nc].to=a;    edge[nc].next=head[b];    head[b]=nc++;}void dfs(int now){    vis[now]=true;    int t,stk[N],top=0,sumc=0,v;    dp[now][1]=0;    for(int i=head[now]; i!=-1; i=edge[i].next)    {        t=edge[i].to;        if(vis[t]) continue;        stk[top++]=t;        dfs(t);        sumc+=dp[t][0];    }    if(top==0)    {        dp[now][0]=dp[now][2]=inf;        dp[now][1]=0;        return;    }    dp[now][1]=sumc;    v=inf;    for(int i=0;i<top;i++)    {        t=stk[i];        v=min(v,sumc-dp[t][0]+min(dp[t][1],dp[t][2]));    }    dp[now][2]=v;    v=inf;    for(int i=0;i<top;i++)    {        int t1=stk[i];        v=min(sumc-dp[t1][0]+dp[t1][2]+1,v);        for(int j=i+1;j<top;j++)        { int t2=stk[j]; v=min(sumc-dp[t1][0]-dp[t2][0]+min(dp[t2][1],dp[t2][2])+min(dp[t1][1],dp[t1][2])+1,v);        }    }    dp[now][0]=v;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        memset(head,-1,sizeof(head));        nc=0;        memset(vis,false,sizeof(vis));        for(int i=1,a,b;i<n;i++)        { scanf("%d%d",&a,&b); add(a,b);        }        dfs(1);        if(dp[1][0]<inf) printf("%dn",dp[1][0]);        else printf("-1n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号