Problem p01062 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 100000

int N, A, B, num_A, num_B;
bool check[NUM];
vector<int> G[NUM];

bool recursive(int side_A, int side_B) {

  if (num_A == N / 2 && num_B == N / 2)
    return true;

  for (int i = 0; i < G[side_A].size(); i++) {

    if (check[G[side_A][i]])
      continue;
    check[G[side_A][i]] = true;

    num_A++;
    for (int k = 0; k < G[side_B].size(); k++) {

      if (check[G[side_B][k]])
        continue;
      check[G[side_B][k]] = true;

      num_B++;
      if (recursive(G[side_A][i], G[side_B][k]))
        return true;

      check[G[side_B][k]] = false;
      num_B--;
    }
    check[G[side_A][i]] = false;
    num_A--;
  }
  return false;
}

int main() {

  scanf("%d %d %d", &N, &A, &B);
  A--;
  B--;
  num_A = num_B = 1;

  for (int i = 0; i < N; i++)
    check[i] = false;

  check[A] = true;
  check[B] = true;

  int from, to;

  for (int loop = 0; loop < N - 1; loop++) {
    scanf("%d %d", &from, &to);
    from--;
    to--;
    G[from].push_back(to);
    G[to].push_back(from);
  }

  if (N % 2 != 0) {
    printf("No\n");
    return 0;
  }

  if (recursive(A, B)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 100000

int N, A, B, num_A, num_B;
bool check[NUM];
vector<int> G[NUM];

bool recursive(int side_A, int side_B) {

  if (num_A == N / 2 && num_B == N / 2)
    return true;

  for (int i = 0; i < G[side_A].size(); i++) {

    if (check[G[side_A][i]])
      continue;
    check[G[side_A][i]] = true;

    num_A++;
    for (int k = 0; k < G[side_B].size(); k++) {

      if (check[G[side_B][k]])
        continue;
      check[G[side_B][k]] = true;

      num_B++;
      if (recursive(G[side_A][i], G[side_B][k]))
        return true;

      check[G[side_B][k]] = false;
      num_B--;
    }
    check[G[side_A][i]] = false;
    num_A--;
  }
  return false;
}

int main() {

  scanf("%d %d %d", &N, &A, &B);
  A--;
  B--;
  num_A = num_B = 1;

  for (int i = 0; i < N; i++)
    check[i] = false;

  check[A] = true;
  check[B] = true;

  int from, to;

  for (int loop = 0; loop < N - 1; loop++) {
    scanf("%d %d", &from, &to);
    from--;
    to--;
    G[from].push_back(to);
    G[to].push_back(from);
  }

  if (N % 2 != 0) {
    printf("No\n");
    return 0;
  }

  int num_3 = 0;
  for (int i = 0; i < N; i++) {
    if (G[i].size() >= 4) {
      printf("No\n");
      return 0;
    }
    if (G[i].size() == 3) {
      num_3++;
      if (num_3 > 2) {
        printf("No\n");
        return 0;
      }
    }
  }

  if (recursive(A, B)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
}