Language: C++

#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1e6 + 50;

inline void read(int &x) {
  x = 0; char ch;
  while (!isdigit(ch = getchar()));
  do { x = (x << 1) + (x << 3) + ch - '0'; }
  while (isdigit(ch = getchar()));
}

struct edge {
  int nxt, to;
} e[N << 1];
int fir[N], cnt = 1;

inline void addedge(int x, int y) {
  e[++ cnt] = (edge) { fir[x], y }; fir[x] = cnt;
}

int n, dp[N], ans = N;
bool z[N];

inline void Dfs(int x, int f) {
  int son = 0, tot = 0;
  bool tmp = 0;
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      son ++;
      Dfs(e[i].to, x);
      tot += dp[e[i].to];
      tmp |= z[e[i].to];
    }
  if (son == 0) dp[x] = z[x] = 1;
  else if (son == 1) dp[x] = tot, z[x] = tmp;
  else dp[x] = tot - tmp;
}

inline void solve(int x, int f, int gift, bool zz) {
  int son = 0, tot = gift, Max = zz;
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      son ++;
      tot += dp[e[i].to];
      Max += z[e[i].to];
    }
  if (son == 0) {
    ans = min(ans, gift + 1 - zz);
    return;
  }
  ans = min(ans, tot - (Max > 0) + 1);
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      if (son == 1)
	solve(e[i].to, x, gift, zz);
      else
	solve(e[i].to, x, tot - (Max > z[e[i].to]) - dp[e[i].to], 0);
    }
}

int deg[N];
      
int main() {
  read(n);
  if (n == 1) return 0 * puts("0");
  for (int i = 1, x, y; i < n; i ++) {
    read(x); read(y);
    addedge(x, y); addedge(y, x);
    deg[x] ++; deg[y] ++;
  }
  int rt = 0;
  for (int i = 1; i <= n; i ++)
    if (deg[i] == 1) { rt = i; break; }
  Dfs(rt, 0);
  ans = dp[rt] + 1;
  solve(e[fir[rt]].to, rt, 1, 1);
  printf("%d\n", ans);
  return 0;
}
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1e6 + 50;

inline void read(int &x) {
  x = 0; char ch;
  while (!isdigit(ch = getchar()));
  do { x = (x << 1) + (x << 3) + ch - '0'; }
  while (isdigit(ch = getchar()));
}

struct edge {
  int nxt, to;
} e[N << 1];
int fir[N], cnt = 1;

inline void addedge(int x, int y) {
  e[++ cnt] = (edge) { fir[x], y }; fir[x] = cnt;
}

int n, dp[N], ans = N;
bool z[N];

inline void Dfs(int x, int f) {
  int son = 0, tot = 0;
  bool tmp = 0;
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      son ++;
      Dfs(e[i].to, x);
      tot += dp[e[i].to];
      tmp |= z[e[i].to];
    }
  if (son == 0) dp[x] = z[x] = 1;
  else if (son == 1) dp[x] = tot, z[x] = tmp;
  else dp[x] = tot - tmp;
}

inline void solve(int x, int f, int gift, bool zz) {
  int son = 0, tot = gift, Max = zz;
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      son ++;
      tot += dp[e[i].to];
      Max += z[e[i].to];
    }
  if (son == 0) {
    ans = min(ans, gift + 1 - zz);
    return;
  }
  ans = min(ans, tot - (Max > 0) + 1);
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      if (son == 1)
	solve(e[i].to, x, gift, zz);
      else
	solve(e[i].to, x, tot - (Max > z[e[i].to]) - dp[e[i].to], 0);
    }
}

int deg[N];
      
int main() {
  read(n);
  if (n == 1) return 0 * puts("0");
  for (int i = 1, x, y; i < n; i ++) {
    read(x); read(y);
    addedge(x, y); addedge(y, x);
    deg[x] ++; deg[y] ++;
  }
  int rt = 0;
  for (int i = 1; i <= n; i ++)
    if (deg[i] == 1) { rt = i; break; }
  Dfs(rt, 0);
  ans = dp[rt] + 1;
  solve(e[fir[rt]].to, rt, 1, 1);
  printf("%d\n", ans);
  return 0;
}

Compiler output:
a.cc:86:11: error: redefinition of 'const int N'
   86 | const int N = 1e6 + 50;
      |           ^
a.cc:5:11: note: 'const int N' previously defined here
    5 | const int N = 1e6 + 50;
      |           ^
a.cc:88:13: error: redefinition of 'void read(int&)'
   88 | inline void read(int &x) {
      |             ^~~~
a.cc:7:13: note: 'void read(int&)' previously defined here
    7 | inline void read(int &x) {
      |             ^~~~
a.cc:95:8: error: redefinition of 'struct edge'
   95 | struct edge {
      |        ^~~~
a.cc:14:8: note: previous definition of 'struct edge'
   14 | struct edge {
      |        ^~~~
a.cc:97:3: error: conflicting declaration 'int e [2000100]'
   97 | } e[N << 1];
      |   ^
a.cc:16:3: note: previous declaration as 'edge e [2000100]'
   16 | } e[N << 1];
      |   ^
a.cc:98:5: error: redefinition of 'int fir [1000050]'
   98 | int fir[N], cnt = 1;
      |     ^~~
a.cc:17:5: note: 'int fir [1000050]' previously declared here
   17 | int fir[N], cnt = 1;
      |     ^~~
a.cc:98:13: error: redefinition of 'int cnt'
   98 | int fir[N], cnt = 1;
      |             ^~~
a.cc:17:13: note: 'int cnt' previously defined here
   17 | int fir[N], cnt = 1;
      |             ^~~
a.cc:100:13: error: redefinition of 'void addedge(int, int)'
  100 | inline void addedge(int x, int y) {
      |             ^~~~~~~
a.cc:19:13: note: 'void addedge(int, int)' previously defined here
   19 | inline void addedge(int x, int y) {
      |             ^~~~~~~
a.cc:104:5: error: redefinition of 'int n'
  104 | int n, dp[N], ans = N;
      |     ^
a.cc:23:5: note: 'int n' previously declared here
   23 | int n, dp[N], ans = N;
      |     ^
a.cc:104:8: error: redefinition of 'int dp [1000050]'
  104 | int n, dp[N], ans = N;
      |        ^~
a.cc:23:8: note: 'int dp [1000050]' previously declared here
   23 | int n, dp[N], ans = N;
      |        ^~
a.cc:104:15: error: redefinition of 'int ans'
  104 | int n, dp[N], ans = N;
      |               ^~~
a.cc:23:15: note: 'int ans' previously defined here
   23 | int n, dp[N], ans = N;
      |               ^~~
a.cc:105:6: error: redefinition of 'bool z [1000050]'
  105 | bool z[N];
      |      ^
a.cc:24:6: note: 'bool z [1000050]' previously declared here
   24 | bool z[N];
      |      ^
a.cc:107:13: error: redefinition of 'void Dfs(int, int)'
  107 | inline void Dfs(int x, int f) {
      |             ^~~
a.cc:26:13: note: 'void Dfs(int, int)' previously defined here
   26 | inline void Dfs(int x, int f) {
      |             ^~~
a.cc:122:13: error: redefinition of 'void solve(int, int, int, bool)'
  122 | inline void solve(int x, int f, int gift, bool zz) {
      |             ^~~~~
a.cc:41:13: note: 'void solve(int, int, int, bool)' previously defined here
   41 | inline void solve(int x, int f, int gift, bool zz) {
      |             ^~~~~
a.cc:144:5: error: redefinition of 'int deg [1000050]'
  144 | int deg[N];
      |     ^~~
a.cc:63:5: note: 'int deg [1000050]' previously declared here
   63 | int deg[N];
      |     ^~~
a.cc:146:5: error: redefinition of 'int main()'
  146 | int main() {
      |     ^~~~
a.cc:65:5: note: 'int main()' previously defined here
   65 | int main() {
      |     ^~~~