#include #include typedef struct Edge { struct Edge *next; int v; } edge; typedef struct { int key, id; } data; typedef struct { data *obj; int size, max_size; } max_heap; void push(max_heap* h, data x) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key > h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(max_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key > h->obj[j].key) j ^= 1; if (h->obj[j].key > h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j <<= 1; } else break; } return output; } int main() { int i, N, u, w, deg[200001] = {}; edge *adj[200001] = {}, e[400001], *p; scanf("%d", &N); if (N == 1) { printf("1\n"); fflush(stdout); return 0; } for (i = 0; i < N - 1; i++) { scanf("%d %d", &u, &w); e[i*2].v = w; e[i*2+1].v = u; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); deg[u]++; deg[w]++; } int par[200001] = {}, q[200001], head, tail; par[1] = 1; q[0] = 1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; q[tail++] = w; } } } int dp[200001]; max_heap h[200001]; data d; for (head--; head >= 0; head--) { u = q[head]; dp[u] = 1; h[u].max_size = deg[u]; h[u].size = 0; h[u].obj = (data*)malloc(sizeof(data) * (h[u].max_size + 1)); for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w == par[u]) continue; d.key = dp[w]; d.id = w; push(&(h[u]), d); } if (h[u].size == 1) { d = pop(&(h[u])); dp[u] += d.key; free(h[u].obj); for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w != par[u]) break; } h[u] = h[w]; if (h[u].size > 0) { d = pop(&(h[u])); dp[u] += d.key; } } else if (h[u].size > 1) { d = pop(&(h[u])); dp[u] += d.key; d = pop(&(h[u])); dp[u] += d.key; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (w == par[u] || h[w].size == 0) continue; if (h[u].size >= h[w].size) { if (h[u].size + h[w].size > h[u].max_size) { h[u].max_size *= 2; h[u].obj = (data*)realloc(h[u].obj, sizeof(data) * (h[u].max_size + 1)); } while (h[w].size > 0) { d = pop(&(h[w])); push(&(h[u]), d); } free(h[w].obj); } else { if (h[u].size + h[w].size > h[w].max_size) { h[w].max_size *= 2; h[w].obj = (data*)realloc(h[w].obj, sizeof(data) * (h[w].max_size + 1)); } while (h[u].size > 0) { d = pop(&(h[u])); push(&(h[w]), d); } free(h[u].obj); h[u] = h[w]; } } } } printf("%d\n", dp[1]); fflush(stdout); return 0; }