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

#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
vector<int> used1, used2;
vector<int> dp1, dp2;
priority_queue<pair<int, int>> pq1;
priority_queue<pair<int, int>> pq2;
vector<vector<int>> v;
int main() {
  int n, m;
  int s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  v = vector<vector<int>>(n);
  // set<pair<int,int>> se;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
    // se.insert(minmax(a,b));
  }
  used1 = vector<int>(n + 10, -1);
  used2 = vector<int>(n + 10, -1);
  pq1.push(make_pair(0, s));
  pq2.push(make_pair(0, t));
  while (pq1.size()) {
    auto top = pq1.top();
    pq1.pop();
    if (used1[top.second] >= 0)
      continue;
    used1[top.second] = -top.first;
    for (int x : v[top.second]) {
      if (used1[x] >= 0)
        continue;
      pq1.push(make_pair(top.first - 1, x));
    }
  }
  while (pq2.size()) {
    auto top = pq2.top();
    pq2.pop();
    if (used2[top.second] >= 0)
      continue;
    used2[top.second] = -top.first;
    for (int x : v[top.second]) {
      if (used2[x] >= 0)
        continue;
      pq2.push(make_pair(top.first - 1, x));
    }
  }
  // for(auto x:se){
  //     cout<<x.first<<" "<<x.second<<endl;
  // }
  long long ans = 0;
  int dist = used1[t];
  // if(dist<2){
  // cout<<0<<endl;
  // return 0;
  // }
  dp1 = vector<int>(n + 10, 0);
  dp2 = vector<int>(n + 10, 0);
  rep(i, n) {
    if (0 <= used1[i] && used1[i] <= n)
      dp1[used1[i]]++;
    if (0 <= used2[i] && used2[i] <= n)
      dp2[used2[i]]++;
  }
  rep(i, dist - 1) {
    // if(i==dist-2-i) ans+=dp1[i]*dp2[dist-2-i]/2;
    ans += (long long)dp1[i] * dp2[dist - 2 - i];
  }
  // rep(i,n){
  // cout<<i<<" "<<used1[i]<<" "<<used2[i]<<endl;
  // if(s==i||t==i) continue;
  // if(used2[i]==dist-2&&se.find(minmax(s,i))==se.end()) ans++;
  // if(used1[i]==dist-2&&se.find(minmax(t,i))==se.end()) ans++;
  // }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
vector<int> used1, used2;
vector<int> dp1, dp2;
priority_queue<pair<int, int>> pq1;
priority_queue<pair<int, int>> pq2;
vector<vector<int>> v;
int main() {
  int n, m;
  int s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  v = vector<vector<int>>(n);
  // set<pair<int,int>> se;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
    // se.insert(minmax(a,b));
  }
  used1 = vector<int>(n + 10, -1);
  used2 = vector<int>(n + 10, -1);
  pq1.push(make_pair(0, s));
  pq2.push(make_pair(0, t));
  while (pq1.size()) {
    auto top = pq1.top();
    pq1.pop();
    if (used1[top.second] >= 0)
      continue;
    used1[top.second] = -top.first;
    for (int x : v[top.second]) {
      if (used1[x] >= 0)
        continue;
      pq1.push(make_pair(top.first - 1, x));
    }
  }
  while (pq2.size()) {
    auto top = pq2.top();
    pq2.pop();
    if (used2[top.second] >= 0)
      continue;
    used2[top.second] = -top.first;
    for (int x : v[top.second]) {
      if (used2[x] >= 0)
        continue;
      pq2.push(make_pair(top.first - 1, x));
    }
  }
  // for(auto x:se){
  //     cout<<x.first<<" "<<x.second<<endl;
  // }
  int ans = 0;
  int dist = used1[t];
  // if(dist<2){
  // cout<<0<<endl;
  // return 0;
  // }
  dp1 = vector<int>(n + 10, 0);
  dp2 = vector<int>(n + 10, 0);
  rep(i, n) {
    dp1[used1[i]]++;
    dp2[used2[i]]++;
  }
  rep(i, dist - 1) {
    // if(i==dist-2-i) ans+=dp1[i]*dp2[dist-2-i]/2;
    ans += dp1[i] * dp2[dist - 2 - i];
  }
  // rep(i,n){
  // cout<<i<<" "<<used1[i]<<" "<<used2[i]<<endl;
  // if(s==i||t==i) continue;
  // if(used2[i]==dist-2&&se.find(minmax(s,i))==se.end()) ans++;
  // if(used1[i]==dist-2&&se.find(minmax(t,i))==se.end()) ans++;
  // }
  cout << ans << endl;
}