标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7477 Accepted Submission(s): 3558
1 # include <bits/stdc++.h> 2 using namespace std; 3 const int MAX = 1550; 4 struct Node 5 { 6 int to; 7 int next; 8 }tree[MAX * 2]; 9 int head[MAX]; 10 int tol = 0; 11 int dp[MAX][2]; 12 void add(int a, int b) 13 { 14 tree[tol].to = b; 15 tree[tol].next = head[a]; 16 head[a] = tol++; 17 18 tree[tol].to = a; 19 tree[tol].next = head[b]; 20 head[b] = tol++; 21 } 22 void dfs(int root, int f) 23 { 24 for(int i = head[root]; i != -1; i = tree[i].next) 25 { 26 int son = tree[i].to; 27 if(son == f) 28 continue; 29 dfs(son, root); 30 dp[root][0] += dp[son][1]; 31 dp[root][1] += min(dp[son][0], dp[son][1]); 32 } 33 } 34 int main() 35 { 36 int n; 37 while(scanf("%d", &n) != EOF) 38 { 39 memset(head, -1, sizeof(head)); 40 memset(dp, 0, sizeof(dp)); 41 tol = 0; 42 for(int i = 1; i <= n; i++) 43 { 44 int a, len; 45 scanf("%d:(%d)", &a, &len); 46 for(int j = 0; j < len; j++) 47 { 48 int b; 49 scanf("%d", &b); 50 add(a + 1, b + 1); 51 } 52 dp[i][1] = 1; 53 } 54 int root = 1; 55 dfs(root, -1); 56 printf("%d\n", min(dp[root][1], dp[root][0])); 57 } 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5794838.html