Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
谁能讲一下与1463的区别,为什么同样的方法这里不行了#include <iostream> #include <stdio.h> #include <cstring> #define N 10005 #define MIN(A,B) ((A)<(B)?(A):(B)) using namespace std; struct edge { int y,next; } path[N*2]; int first[N],p,dp[N][2],n; inline void add(int x,int y) { path[p].y=y; path[p].next=first[x]; first[x]=p++; path[p].y=x; path[p].next=first[y]; first[y]=p++; } int min(int x,int y) { return x<y?x:y; } int dfs(int root,int state,int pere) { if(dp[root][state]>=0)return dp[root][state]; int ans=state,t,tmp; for(int i=first[root];i!=-1;i=path[i].next) { t=path[i].y; if(t==pere)continue; if(state==1) { tmp=min(dfs(t,0,root),dfs(t,1,root)); }else tmp=dfs(t,1,root); ans+=tmp; } return dp[root][state]=ans; } int main() { freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int x,y; memset(first,-1,sizeof(first)); p=0; for(int i=1; i<n; i++) { scanf("%d %d",&x,&y); add(x,y); } memset(dp,-1,sizeof(dp)); int a=dfs(1,0,-1); int b=dfs(1,1,-1); printf("%d\n",a<b?a:b); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator