Summary: There is a tree-like structure of the network, in some nodes to install the server so that the server is not a node around exactly a server, ask the minimum number of servers.
First turn to have a root tree (when enumerating the child nodes ignore their father), and then Dp[u][s] represents the minimum number of child tree installation Server, the impact of the decision is whether you are the server and the state of its parent node, so the value of S is U is the server (0), U is not the server U father is the server (1), U is not the father of the server U nor the server (2).
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4+4;intN;vector<int>G[MAXN];#definePB push_backintdp[maxn][3];voidDfsintUintFA) {dp[u][1] =0; dp[u][0] =1; intSZ =g[u].size (); if(SZ = =0) {dp[u][2] =-MAXN;//illegal return; } for(inti =0; I < sz; i++){ intv =G[u][i]; if(v = = FA)Continue; DFS (V,U); dp[u][0] + = min (dp[v][0],dp[v][1]); dp[u][1] + = dp[v][2]; } dp[u][2] =MAXN; int&D2 = dp[u][2]; for(inti =0; I < sz; i++){ intv =G[u][i]; if(v = = FA)Continue; D2= Min (d2,dp[u][1]-dp[v][2]+dp[v][0]); }}voidinit () {scanf ("%d",&N); for(inti =1; I <= N; i++) g[i].clear (); for(inti =1; I < n;i++){ intU,v; scanf"%d%d",&u,&v); G[u]. PB (v); G[V]. PB (U); }}intMain () {//freopen ("In.txt", "R", stdin); intFlag; Do{init (); DFS (1,-1); intans = min (dp[1][0],dp[1][2]); printf ("%d\n", ans); scanf ("%d",&flag); } while(Flag = =0); return 0;}
UVA 1218-perfect Service