Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
PROG: apc001e
LANG: C++11
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
 */
#include <bits/stdc++.h>

using namespace std;

long long readi() {
  long long input = 0;
  bool negative = false;
  char c = ' ';
  while (c < '-') {
    c = getchar();
  }
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  while (c >= '0') {
    input = 10 * input + (c - '0');
    c = getchar();
  }
  if (negative) {
    input = -input;
  }
  return input;
}
void printi(long long output) {
  if (output == 0) {
    putchar('0');
    return;
  }
  if (output < 0) {
    putchar('-');
    output = -output;
  }
  int aout[20];
  int ilen = 0;
  while (output) {
    aout[ilen] = ((output % 10));
    output /= 10;
    ilen++;
  }
  for (int i = ilen - 1; i >= 0; i--) {
    putchar(aout[i] + '0');
  }
  return;
}
string reads() {
  string input = "";
  char c = ' ';
  while (c <= ' ') {
    c = getchar();
  }
  while (c > ' ') {
    input += c;
    c = getchar();
  }
  return input;
}
int sgn(long long x) {
  if (x < 0) {
    return -1;
  }
  if (x > 0) {
    return 1;
  }
  return 0;
}
long long randomize(long long mod) {
  return ((1 << 30) * rand() + (1 << 15) * rand() + rand()) % mod;
}

#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-20;

#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100010

long long normalize(long long x, long long mod = INF) {
  return (((x % mod) + mod) % mod);
}

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N;
vector<int> edge[MAXN];
int parent[MAXN];
bool line[MAXN];
int ans;

void dfs_par(int u) {
  for (int v : edge[u]) {
    if (v == parent[u]) {
      continue;
    }
    parent[v] = u;
    dfs_par(v);
  }
}
void solve(int u) {
  int lines = 0;
  for (int v : edge[u]) {
    if (v == parent[u]) {
      continue;
    }
    if (line[v]) {
      lines++;
    } else {
      solve(v);
    }
  }
  //	cerr << u << ' ' << lines << endl;
  ans += max(0, lines - 1);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  srand(time(0));
  //	cout << fixed << setprecision(12);
  //	cerr << fixed << setprecision(12);
  if (fopen("apc001e.in", "r")) {
    freopen("apc001e.in", "r", stdin);
    freopen("apc001e.out", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].PB(v);
    edge[v].PB(u);
  }
  int root = -1;
  for (int i = 0; i < N; i++) {
    if (edge[i].size() > 2) {
      root = i;
    }
  }
  if (root == -1) {
    cout << "1\n";
    return 0;
  }
  dfs_par(root);
  for (int i = 0; i < N; i++) {
    if (edge[i].size() > 1) {
      continue;
    }
    int v = i;
    while (edge[v].size() <= 2) {
      line[v] = true;
      v = parent[v];
    }
  }
  solve(root);
  cout << ans << '\n';
  //	cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << "
  //ms" << endl;
  return 0;
}


Fixed submission:
/*
PROG: apc001e
LANG: C++11
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
 */
#include <bits/stdc++.h>

using namespace std;

long long readi() {
  long long input = 0;
  bool negative = false;
  char c = ' ';
  while (c < '-') {
    c = getchar();
  }
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  while (c >= '0') {
    input = 10 * input + (c - '0');
    c = getchar();
  }
  if (negative) {
    input = -input;
  }
  return input;
}
void printi(long long output) {
  if (output == 0) {
    putchar('0');
    return;
  }
  if (output < 0) {
    putchar('-');
    output = -output;
  }
  int aout[20];
  int ilen = 0;
  while (output) {
    aout[ilen] = ((output % 10));
    output /= 10;
    ilen++;
  }
  for (int i = ilen - 1; i >= 0; i--) {
    putchar(aout[i] + '0');
  }
  return;
}
string reads() {
  string input = "";
  char c = ' ';
  while (c <= ' ') {
    c = getchar();
  }
  while (c > ' ') {
    input += c;
    c = getchar();
  }
  return input;
}
int sgn(long long x) {
  if (x < 0) {
    return -1;
  }
  if (x > 0) {
    return 1;
  }
  return 0;
}
long long randomize(long long mod) {
  return ((1 << 30) * rand() + (1 << 15) * rand() + rand()) % mod;
}

#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-20;

#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100010

long long normalize(long long x, long long mod = INF) {
  return (((x % mod) + mod) % mod);
}

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N;
vector<int> edge[MAXN];
int parent[MAXN];
bool line[MAXN];
int ans;

void dfs_par(int u) {
  for (int v : edge[u]) {
    if (v == parent[u]) {
      continue;
    }
    parent[v] = u;
    dfs_par(v);
  }
}
void solve(int u) {
  int lines = 0;
  for (int v : edge[u]) {
    if (v == parent[u]) {
      continue;
    }
    if (line[v]) {
      lines++;
    } else {
      solve(v);
    }
  }
  //	cerr << u << ' ' << lines << endl;
  ans += max(0, lines - 1);
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  srand(time(0));
  //	cout << fixed << setprecision(12);
  //	cerr << fixed << setprecision(12);
  if (fopen("apc001e.in", "r")) {
    freopen("apc001e.in", "r", stdin);
    freopen("apc001e.out", "w", stdout);
  }
  cin >> N;
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].PB(v);
    edge[v].PB(u);
  }
  int root = -1;
  for (int i = 0; i < N; i++) {
    if (edge[i].size() > 2) {
      root = i;
    }
  }
  if (root == -1) {
    cout << "1\n";
    return 0;
  }
  parent[root] = N;
  dfs_par(root);
  for (int i = 0; i < N; i++) {
    if (edge[i].size() > 1) {
      continue;
    }
    int v = i;
    while (edge[v].size() <= 2) {
      line[v] = true;
      v = parent[v];
    }
  }
  solve(root);
  cout << ans << '\n';
  //	cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << "
  //ms" << endl;
  return 0;
}