[题目链接]
http://poj.org/problem?id=1463
[算法]
树形DP
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 1510 int i,n,tot,u,s,v; int f[MAXN][2]; int head[MAXN]; struct Edge { int to,nxt; } e[MAXN << 1]; inline void addedge(int u,int v) { tot++; e[tot] = (Edge){v,head[u]}; head[u] = tot; } inline void dp(int u,int fa) { int i,v; f[u][0] = 0; f[u][1] = 1; for (i = head[u]; i; i = e[i].nxt) { v = e[i].to; if (v == fa) continue; dp(v,u); f[u][1] += min(f[v][0],f[v][1]); f[u][0] += f[v][1]; } } int main() { while (scanf("%d",&n) != EOF) { tot = 0; for (i = 0; i < n; i++) head[i] = 0; for (i = 1; i <= n; i++) { scanf("%d : (%d)",&u,&s); while (s--) { scanf("%d",&v); addedge(u,v); addedge(v,u); } } dp(0,-1); printf("%d ",min(f[0][0],f[0][1])); } return 0; }