# JZOJ 5944

### 题目大意

简单的手玩后发现，一棵以 x 为根的子树，x 有 k 个儿子

代码：

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cstdio>
using namespace std;

const int MAX_N = 1000005;

struct EDGE{
int nxt, to;
explicit EDGE(int NXT = 0, int TO = 0) {nxt = NXT; to = TO;}
}edge[MAX_N << 1];
int n, tot_edge, ans, bgn;

inline int rd() {
register int x = 0, c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = x * 10 + (c ^ 48);
c = getchar();
}
return x;
}
inline void add(int x, int y) {
++deg[x];
++deg[y];
return;
}
bool dfs(int x, int frm) {
int tot = 0;
for (int i = head[x]; i; i = edge[i].nxt) if (edge[i].to != frm) {
int y = edge[i].to;
tot += dfs(y, x);
}
if (frm == 0) {
if (tot < deg[x] - 1) {
ans += (deg[x] - 1 - tot);
tot += (deg[x] - 1 - tot);
}
} else if (tot < deg[x] - 2) {
ans += (deg[x] - 2 - tot);
tot += (deg[x] - 2 - tot);
}
return (tot != 0);
}

int main() {
freopen("beacon.in", "r", stdin);
freopen("beacon.out", "w", stdout);
n = rd();
register int xx, yy;
for (int i = 1; i < n; ++i) {
xx = rd(); yy = rd();
}
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) {
if (deg[i] > 2) {
bgn = i;
break;
}
}
if (!bgn) {
puts("1");
return 0;
}
dfs(bgn, 0);
printf("%d\n", ans);
return 0;
}

posted @ 2018-11-02 19:39  AWordThatWeDefine  阅读(180)  评论(0编辑  收藏  举报