Problem p01062 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef int ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
P es[N];
int col[N];
int deg[N];

void dfs(ll v, ll pre) {
  cnt[v] = 1;
  for (ll nv : g[v]) {
    if (nv == pre)
      continue;
    dfs(nv, v);
    cnt[v] += cnt[nv];
  }
}

void ddfs(ll v, ll pre, ll c) {
  col[v] = c;
  for (ll nv : g[v]) {
    if (nv == pre)
      continue;
    ddfs(nv, v, c);
  }
}

bool solve() {
  int n, x, y;
  scanf("%d%d%d", &n, &x, &y);
  x--;
  y--;

  memset(deg, 0, sizeof(deg));

  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    es[i] = {a, b};
    deg[a]++;
    deg[b]++;
  }

  if (n == 2)
    return true;
  if (n % 2 == 1)
    return false;

  dfs(0, -1);
  memset(col, -1, sizeof(col));
  rep(i, n - 1) {
    ll a = es[i].fi, b = es[i].se;
    if (cnt[a] < cnt[b])
      swap(a, b);
    if (cnt[b] == n - cnt[b]) {
      ddfs(a, b, 0);
      ddfs(b, a, 1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if (col[x] == col[y])
    return false;

  int cnt1[2] = {0, 0};
  rep(i, n) {
    if (deg[i] > 2)
      return false;
    if (deg[i] == 1)
      cnt1[col[i]]++;
  }

  if (cnt1[0] != 2 || cnt1[1] != 2)
    return false;

  return (deg[x] == 1 && deg[y] == 1);
}

int main() {
  if (solve())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n, x, y;
vector<int> g[100101];
int cnt[100010];
vector<P> es;
int col[100011];
int deg[100010];

void dfs(int v, int pre) {
  cnt[v] = 1;
  for (int nv : g[v]) {
    if (nv == pre)
      continue;
    dfs(nv, v);
    cnt[v] += cnt[nv];
  }
}

void ddfs(int v, int pre, int c) {
  col[v] = c;
  for (int nv : g[v]) {
    if (nv == pre)
      continue;
    ddfs(nv, v, c);
  }
}

int main() {
  scanf("%d%d%d", &n, &x, &y);
  x--;
  y--;
  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    es.push_back(P(a, b));
    deg[a]++;
    deg[b]++;
  }
  if (n == 2) {
    cout << "Yes" << endl;
    return 0;
  }
  if (n % 2 == 1) {
    cout << "No" << endl;
    return 0;
  }
  dfs(0, -1);
  memset(col, -1, sizeof(col));
  for (P e : es) {
    ll a = e.fi, b = e.se;
    if (cnt[a] < cnt[b])
      swap(a, b);
    if (cnt[b] == n - cnt[b]) {
      ddfs(a, b, 0);
      ddfs(b, a, 1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }
  if (col[0] == -1 || col[x] == col[y]) {
    cout << "No" << endl;
    return 0;
  }
  {
    int cnt1 = 0;
    rep(i, n) {
      if (col[i] == 0) {
        if (deg[i] > 2) {
          cout << "No" << endl;
          return 0;
        }
        if (deg[i] == 1)
          cnt1++;
      }
    }
    if (cnt1 != 2) {
      cout << "No" << endl;
      return 0;
    }
  }
  {
    int cnt1 = 0;
    rep(i, n) {
      if (col[i] == 1) {
        if (deg[i] > 2) {
          cout << "No" << endl;
          return 0;
        }
        if (deg[i] == 1)
          cnt1++;
      }
    }
    if (cnt1 != 2) {
      cout << "No" << endl;
      return 0;
    }
  }
  if (deg[x] == 1 && deg[y] == 1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}