Problem p03990 (C++)

Buggy submission (status: Runtime Error):
// Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;
// #include<bits/stweight++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 2e5 + 5;
const int lg = 20;
vector<int> red[N];
vector<int> blue[N];
int h[N];
int par[lg + 5][N];
int n, x, y;
bool winPos[N];
int disR[N];
int disB[N];

void dfs(int v = 0, int p = -1) {
  for (int u : blue[v]) {
    if (u != p) {
      par[0][u] = v;
      h[u] = h[v] + 1;
      dfs(u, v);
    }
  }
}

void pre() {
  for (int k = 1; k < lg; k++) {
    for (int i = 0; i < n; i++) {
      par[k][i] = par[k - 1][par[k - 1][i]];
    }
  }
}

int lca(int v, int u) {
  if (h[v] > h[u]) {
    swap(v, u);
  }
  for (int i = 0; i < lg; i++) {
    if (h[u] - h[v] >> i & 1) {
      u = par[i][u];
    }
  }
  for (int i = lg - 1; i >= 0; i--) {
    if (par[i][v] != par[i][u]) {
      v = par[i][v], u = par[i][u];
    }
  }
  return v == u ? v : par[0][v];
}

void dfs1(int v, int par = -1) {
  if (disR[v] >= disB[v]) {
    return;
  }
  for (auto u : red[v]) {
    if (u != par) {
      disR[u] = disR[v] + 1;
      dfs1(u, v);
    }
  }
}

void dfs2(int v, int par = -1) {
  for (auto u : blue[v]) {
    if (u != par) {
      disB[u] = disB[v] + 1;
      dfs2(u, v);
    }
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    red[u - 1].push_back(v - 1);
    red[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    blue[u - 1].push_back(v - 1);
    blue[v - 1].push_back(u - 1);
  }
  dfs();
  pre();
  for (int i = 0; i < n; i++) {
    for (auto u : red[i]) {
      int tmp = lca(u, i);
      if (h[u] - h[tmp] + h[i] - h[tmp] > 2) {
        winPos[i] = true;
        winPos[u] = true;
      }
    }
  }
  memset(disR, 127, sizeof disR);
  disR[x - 1] = 0;
  dfs2(y - 1);
  dfs1(x - 1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (winPos[i] && disR[i] <= disB[i]) {
      assert(1 != 1);
      cout << -1 << endl;
      exit(0);
    }
    if (disR[i] < N) {
      // cout << endl;
      // cout << i << " " << disB[i] << endl;
      ans = max(ans, disB[i] * 2);
    }
  }
  // cout << (1 << 20) << endl;
  cout << ans << endl;
}


Fixed submission:
// Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;
// #include<bits/stweight++.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 2e5 + 5;
const int lg = 20;
vector<int> red[N];
vector<int> blue[N];
int h[N];
int par[lg + 5][N];
int n, x, y;
bool winPos[N];
int disR[N];
int disB[N];

void dfs(int v = 0, int p = -1) {
  for (int u : blue[v]) {
    if (u != p) {
      par[0][u] = v;
      h[u] = h[v] + 1;
      dfs(u, v);
    }
  }
}

void pre() {
  for (int k = 1; k < lg; k++) {
    for (int i = 0; i < n; i++) {
      par[k][i] = par[k - 1][par[k - 1][i]];
    }
  }
}

int lca(int v, int u) {
  if (h[v] > h[u]) {
    swap(v, u);
  }
  for (int i = 0; i < lg; i++) {
    if (h[u] - h[v] >> i & 1) {
      u = par[i][u];
    }
  }
  for (int i = lg - 1; i >= 0; i--) {
    if (par[i][v] != par[i][u]) {
      v = par[i][v], u = par[i][u];
    }
  }
  return v == u ? v : par[0][v];
}

void dfs1(int v, int par = -1) {
  if (disR[v] >= disB[v]) {
    return;
  }
  for (auto u : red[v]) {
    if (u != par) {
      disR[u] = disR[v] + 1;
      dfs1(u, v);
    }
  }
}

void dfs2(int v, int par = -1) {
  for (auto u : blue[v]) {
    if (u != par) {
      disB[u] = disB[v] + 1;
      dfs2(u, v);
    }
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    red[u - 1].push_back(v - 1);
    red[v - 1].push_back(u - 1);
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    blue[u - 1].push_back(v - 1);
    blue[v - 1].push_back(u - 1);
  }
  dfs();
  pre();
  for (int i = 0; i < n; i++) {
    for (auto u : red[i]) {
      int tmp = lca(u, i);
      if (h[u] - h[tmp] + h[i] - h[tmp] > 2) {
        winPos[i] = true;
        winPos[u] = true;
      }
    }
  }
  memset(disR, 127, sizeof disR);
  disR[x - 1] = 0;
  dfs2(y - 1);
  dfs1(x - 1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (winPos[i] && disR[i] < disB[i]) {
      cout << -1 << endl;
      exit(0);
    }
    if (disR[i] < N) {
      // cout << endl;
      // cout << i << " " << disB[i] << endl;
      ans = max(ans, disB[i] * 2);
    }
  }
  // cout << (1 << 20) << endl;
  cout << ans << endl;
}