HDU 1054 Strategic Game (树形DP)

来源:互联网 发布:java管理系统 编辑:程序博客网 时间:2024/04/30 14:41

Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7092    Accepted Submission(s): 3359


Problem 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?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree: 

 

the solution is one soldier ( at the node 1).

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 table:
 

Sample Input
40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)
 

Sample Output
12
 

Source
Southeastern Europe 2000
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1068 1053 1150 1151 1281 

代码:
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;const int maxn=16000;vector<int>G[maxn];int dp[maxn][2];void DFS(int u,int fa){    dp[u][1]=1;    dp[u][0]=0;    for(int i=0;i<G[u].size();i++)    {        int v=G[u][i];        if(v==fa)continue;        DFS(v,u);        dp[u][1]=dp[u][1]+min(dp[v][1],dp[v][0]);        dp[u][0]=dp[u][0]+dp[v][1];    }}int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<=maxn;i++)G[i].clear();        for(int i=0;i<=1500;i++)        {            dp[i][0]=dp[i][1]=0;        }        int root=-1;        for(int i=0;i<n;i++)        {            int t,k;            char a,b,c;            scanf("%d",&t);            scanf("%c%c%d%c",&a,&b,&k,&c);            for(int j=0;j<k;j++)            {                int x;                scanf("%d",&x);                G[t].push_back(x);            }            if(k!=0&&root==-1)                 // 根一定是第一次出现且k不为0的   t;建的是单向图                root=t;        }        DFS(root,-1);        printf("%d\n",min(dp[root][1],dp[root][0]));    }}

代码:
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;const int maxn=16000;vector<int>G[maxn];int dp[maxn][2];void DFS(int u,int fa){    dp[u][1]=1;    dp[u][0]=0;    for(int i=0;i<G[u].size();i++)    {        int v=G[u][i];        if(v==fa)continue;        DFS(v,u);        dp[u][1]=dp[u][1]+min(dp[v][1],dp[v][0]);        dp[u][0]=dp[u][0]+dp[v][1];    }}int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<=maxn;i++)G[i].clear();        for(int i=0;i<=1500;i++)        {            dp[i][0]=dp[i][1]=0;        }        int root=-1;        for(int i=0;i<n;i++)        {            int t,k;            char a,b,c;            scanf("%d",&t);            scanf("%c%c%d%c",&a,&b,&k,&c);            for(int j=0;j<k;j++)            {                int x;                scanf("%d",&x);                G[t].push_back(x);                G[x].push_back(t);            }            if(k!=0)                root=t;        }        DFS(root,-1);        printf("%d\n",min(dp[root][1],dp[root][0]));        //printf("%d %d",dp[3][0],dp[3][1]);    }}


0 0