Problem p01062 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

void dfs(const vector<vector<int>> &edges, int now, int pre, int rest,
         vector<int> &ans, bool flag) {

  ans[now] = true;
  if (rest == 0)
    return;
  for (auto e : edges[now]) {
    if (e == pre)
      continue;
    else {
      if (flag && edges[now].size() == 3) {
        flag = false;
        continue;
      }
      dfs(edges, e, now, rest - 1, ans, flag);
      return;
    }
  }
  return;
}

int main() {
  int N, S, T;
  cin >> N >> S >> T;
  S--;
  T--;

  vector<vector<int>> edges(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  bool ok = false;

  if (N == 2) {
    ok = true;
  } else if (N % 2 == 1 || edges[S].size() > 2 || edges[T].size() > 2) {
    ok = false;
  } else {
    if (count_if(edges.begin(), edges.end(),
                 [](const vector<int> &v) { return v.size() >= 4; }) >= 1) {
      ok = false;
    } else if (count_if(edges.begin(), edges.end(), [](const vector<int> &v) {
                 return v.size() >= 3;
               }) >= 3) {
      ok = false;
    } else {
      for (int i = 0; i < edges[S].size() * 2; ++i) {
        for (int j = 0; j < edges[T].size() * 2; ++j) {
          vector<int> vs, vt;
          vs.resize(N);
          vt.resize(N);
          vs[S] = true;
          vt[T] = true;
          dfs(edges, edges[S][i / 2], S, N / 2 - 2, vs, i % 2 == 0);
          dfs(edges, edges[T][j / 2], T, N / 2 - 2, vt, j % 2 == 0);

          bool aok = true;
          for (int k = 0; k < N; ++k) {
            if (!vs[k] && !vt[k])
              aok = false;
          }
          if (aok)
            ok = true;
        }
      }
    }
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

void dfs(const vector<vector<int>> &edges, int now, int pre, int rest,
         vector<int> &ans) {

  ans[now] = true;
  if (rest == 0)
    return;
  for (auto e : edges[now]) {
    if (e == pre)
      continue;
    else {
      dfs(edges, e, now, rest - 1, ans);
      return;
    }
  }
  return;
}

int main() {
  int N, S, T;
  cin >> N >> S >> T;
  S--;
  T--;

  vector<vector<int>> edges(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  bool ok = false;

  if (N == 2) {
    ok = true;
  } else if (edges[S].size() > 2 || edges[T].size() > 2) {

  } else {
    for (int i = 0; i < edges[S].size(); ++i) {
      for (int j = 0; j < edges[T].size(); ++j) {
        vector<int> vs, vt;
        vs.resize(N);
        vt.resize(N);
        vs[S] = true;
        vt[T] = true;
        dfs(edges, edges[S][i], S, N / 2 - 2, vs);
        dfs(edges, edges[T][j], T, N / 2 - 2, vt);

        assert(count(vs.begin(), vs.end(), 1) == N / 2 &&
               count(vt.begin(), vt.end(), 1) == N / 2);
        bool aok = true;
        for (int k = 0; k < N; ++k) {
          if (!vs[k] && !vt[k])
            aok = false;
        }
        if (aok)
          ok = true;
      }
    }
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}