HDU 1054 - Strategic Game
http://blog.csdn.net/u014141559/article/details/38125679
如果当前节点站士兵,则以它为父亲的子节点(就是它的邻接点)就可以站或者不站,取
两种情况中消耗最小的。 dp[i][1]+=min(dp[v][1],dp[v][0])
如果当前节点不站士兵,则它的邻接点不能被守卫到,故它的邻接点必须要有士兵。
dp[i][0]+=dp[v][1];
v是i的邻接点。
跟那个, poj 2342 Anniversary party(简单树形dp+dfs)真是像极了。。。
我就是根据那个来写的。。。
加边的时候当无向边处理。所以不要忘了add(b,a)!!
vector<int> t[1510];
hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)
最小顶点覆盖,一直超时,后来才意识到图是一个稀疏图,用矩阵存图果断超时。用邻接表轻松AC!
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:
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
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
题目分析:
用最少的士兵将所有的道路都监控起来
这题是最小顶点覆盖,可以用二分匹配,但是今天学树形DP,走起~~
基础模板:
1、建图,用邻接表
2、dfs,遍历整个图,直到叶子,然后子问题一层一层往上合并成整个问题
状态转移 dp[i][0] i节点不放士兵所需要的最少方案数,dp[i][1] i节点放士兵的最少方案数
每个节点都有两个状态,放士兵或者不放士兵
1.如果该节点不放士兵,那么它的所有子节点必须放
2.如果该节点放士兵,那么它的子节点有两种决策,放或不放,去最小的
http://blog.csdn.net/u012841845/article/details/18450551如果当前节点站士兵,则以它为父亲的子节点(就是它的邻接点)就可以站或者不站,取
两种情况中消耗最小的。 dp[i][1]+=min(dp[v][1],dp[v][0])
如果当前节点不站士兵,则它的邻接点不能被守卫到,故它的邻接点必须要有士兵。
dp[i][0]+=dp[v][1];
v是i的邻接点。
跟那个, poj 2342 Anniversary party(简单树形dp+dfs)真是像极了。。。
我就是根据那个来写的。。。
加边的时候当无向边处理。所以不要忘了add(b,a)!!
- struct edge
- {
- int to;
- int next;
- }w[10010];
- bool vis[1510];
- int dp[1510][2],head[1510],tot;
- void add(int x,int y)
- {
- w[tot].to=y;
- w[tot].next=head[x];
- head[x]=tot++;
- }
- void dfs(int r)
- {
- vis[r]=true;
- int v,i;
- for(i=head[r];i;i=w[i].next)
- {
- v=w[i].to;
- if(!vis[v])
- {
- dfs(v);
- dp[r][0]+=dp[v][1];
- dp[r][1]+=min(dp[v][1],dp[v][0]);
- }
- }
- }
- int main()
- {
- int n,m,a,b;
- while(scanf("%d",&n)!=EOF)
- {
- memset(vis,0,sizeof(vis));
- memset(head,0,sizeof(head));
- tot=1;
- for(int i=0;i<n;i++)
- {
- dp[i][0]=0;
- dp[i][1]=1;
- }
- for(int i=0;i<n;i++)
- {
- scanf("%d:(%d)",&a,&m);
- for(int i=0;i<m;i++)
- {
- scanf("%d",&b);
- add(a,b);
- add(b,a);
- }
- }
- dfs(0);
- printf("%d\n",min(dp[0][0],dp[0][1]));
- }
- return 0;
- }
vector<int> t[1510];
hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)
最小顶点覆盖,一直超时,后来才意识到图是一个稀疏图,用矩阵存图果断超时。用邻接表轻松AC!
- int mark[N],n,t;
- int link[N],head[N];
- struct node
- {
- int u,v,next;
- }g[N*10];
- void add(int u,int v)
- {
- g[t].u=u;
- g[t].v=v;
- g[t].next=head[u];
- head[u]=t++;
- }
- int find(int u)
- {
- int i,v;
- for(i=head[u];i!=-1;i=g[i].next)
- {
- v=g[i].v;
- if(!mark[v])
- {
- mark[v]=1;
- if(link[v]==-1||find(link[v]))
- {
- link[v]=u;
- return 1;
- }
- }
- }
- return 0;
- }
- int main()
- {
- int i,u,q,v;
- while(scanf("%d",&n)!=-1)
- {
- memset(head,-1,sizeof(head));
- memset(link,-1,sizeof(link));
- t=0;
- for(i=0;i<n;i++)
- {
- scanf("%d:(%d)",&u,&q);
- while(q--)
- {
- scanf("%d",&v);
- add(u,v);
- add(v,u); //建立的双向边,所以答案最后除以二
- }
- }
- int ans=0;
- for(i=0;i<n;i++)
- {
- memset(mark,0,sizeof(mark));
- ans+=find(i);
- }
- printf("%d\n",ans/2);
- }
- return 0;
- }