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

poj 3763 Tour in Wonder Land

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

poj 3763 Tour in Wonder Land

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std ;const int N = 100001 ;const int Inf = 100000000 ;struct Node{       int vdx ;       Node *next ;   }*map[N],nome[2*N] ; int n,en;void  add(int from,int to){      Node *ptr = &nome[en++] ;      ptr ->vdx = to ;      ptr ->next = map[from] ;      map[from] = ptr ;    }   int   son[N],dp[N][2],sug[N];bool  used[N] ;int   cmp(int a,int b){      return  dp[a][0]-dp[a][1] < dp[b][0]-dp[b][1] ;   }void  dfs(int u,int f){      int v , tot = 0 ;        used[u] = true ;      dp[u][0] = dp[u][1] = Inf ;      for(Node *ptr=map[u];ptr;ptr=ptr->next)      {   v = ptr -> vdx ;     if(!used[v])   {son[u] ++ ;dfs(v,u) ;   tot += dp[v][1] ;    }         }      if(son[u] == 0)      {          dp[u][0] = dp[u][1] = 0 ;         return ;       }       int j = 0 ;        for(Node *ptr=map[u];ptr;ptr=ptr->next)      {   int v = ptr -> vdx ;   if(v != f)  {        dp[u][0] = min(dp[u][0],tot-dp[v][1]+dp[v][0]+son[u]-1) ;         sug[j++] = v ;    }      }       sort(sug,sug+j,cmp) ;      if(j==1) {dp[u][1] = dp[u][0] ;      }else {          int x = sug[0] , y = sug[1] ;          dp[u][1] = tot - dp[x][1] - dp[y][1] + dp[x][0] + dp[y][0] + son[u]-2 ;    dp[u][1] = min(dp[u][0],dp[u][1]) ;      } } int readT(){      char c;int ret;      while(c=getchar(),c<'0'||c>'9');      ret=c-'0';      while(c=getchar(),c>='0'&&c<='9')ret=ret*10+c-'0';      return ret;}int   main(){      while(~scanf("%d",&n))      { int a,b ; en=0;  for(int i=1;i<=n;i++)  map[i] = NULL ;  for(int i=1;i<n;i++) {     a = readT() ;     b = readT() ;       add(a,b) ;     add(b,a) ;  } memset(son,0,sizeof(son)) ; memset(used,false,sizeof(used)) ;    dfs(1,-1) ; printf("%dn",dp[1][1]+1) ;       }      return 0 ; }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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