main.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int N = 1500;
  6. const int INF = 0x3F3F3F3F;
  7. struct Node {
  8. short par;
  9. short cnt;
  10. short nxt[10];
  11. } tree[N];
  12. int dp[N][2];
  13. void tree_dp(int id) {
  14. if (tree[id].cnt == 0) {
  15. dp[id][true] = 1;
  16. dp[id][false] = 0;
  17. return;
  18. }
  19. for (int i = 0, c = tree[id].cnt; i < c; i++) {
  20. int nxt = tree[id].nxt[i];
  21. tree_dp(nxt);
  22. if (i == 0) {
  23. dp[id][false] = dp[nxt][true];
  24. dp[id][true] = min(dp[nxt][true], dp[nxt][false]) + 1;
  25. } else {
  26. dp[id][false] += dp[nxt][true];
  27. dp[id][true] += min(dp[nxt][true], dp[nxt][false]);
  28. }
  29. }
  30. return;
  31. }
  32. int main() {
  33. int n, a, b, c;
  34. while (scanf("%d", &n) != EOF) {
  35. for (int i = 0; i < n; i++) tree[i].par = -1;
  36. for (int i = 0; i < n; i++) {
  37. scanf("%d:(%d)", &a, &b);
  38. tree[a].cnt = b;
  39. for (int j = 0; j < b; j++) {
  40. scanf("%d", &c);
  41. tree[a].nxt[j] = c;
  42. tree[c].par = a;
  43. }
  44. }
  45. memset(dp, 0x3F, sizeof(dp));
  46. int root = 0;
  47. while (tree[root].par != -1) root = tree[root].par;
  48. tree_dp(root);
  49. printf("%d\n", min(dp[root][true], dp[root][false]));
  50. }
  51. return 0;
  52. }