點擊打開鏈接 Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 6105 Accepted: 2808
Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?Input
The input contains several data sets in text format. Each data set represents a tree with the following description:Output
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:Sample Input
4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)
Sample Output
1 2
Source
Southeastern Europe 2000//6508K 375MS #include#include #include #define M 1507 using namespace std; int head[M],nodes; int dp[M][M]; struct E { int v,next; }edge[M*M]; void addedge(int u,int v) { edge[nodes].v=v;edge[nodes].next=head[u]; head[u]=nodes++; } void DP(int u,int p) { dp[u][0]=1; dp[u][1]=0; int k,v; for(k=head[u];k!=-1;k=edge[k].next) { v=edge[k].v; if(v==p)continue; DP(v,u); dp[u][0]+=min(dp[v][0],dp[v][1]); dp[u][1]+=dp[v][0]; } } int main() { int n; while(scanf("%d",&n)!=EOF) { memset(head,-1,sizeof(head)); nodes=0; int u,k,v; for(int i=0;i