#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <set> #include <stack> #include <map> #define LL long long #define FOR(i, x, y) for(int i=x;i<=y;i++) using namespace std; const int MAXN = 11000 + 10; const int INF = 10000000; int dp[MAXN][3]; int N; struct Edge { int to, next; }edge[MAXN * 2]; int head[MAXN]; int tot = 0; void addedge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void init() { memset(head, -1, sizeof(head)); tot = 0; FOR(i, 1, N) { dp[i][0] = 1; dp[i][1] = 0; dp[i][2] = INF; } } void dfs(int u, int pre) { for(int i=head[u]; i!=-1; i=edge[i].next) { int v = edge[i].to; if(v == pre) continue; dfs(v, u); dp[u][0] += min(dp[v][0], dp[v][1]); dp[u][1] += dp[v][2]; dp[u][2] = min(dp[u][2], dp[v][0] - dp[v][2]); } dp[u][2] += dp[u][1]; } int main() { while(scanf("%d", &N)!=EOF) { if(N == 0) continue; if(N == -1) break; init(); int u, v; for(int i=1;i<N;i++) { scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, 0); printf("%d\n", min(dp[1][0], dp[1][2])); } return 0; }
时间: 2024-10-05 16:39:53