Problem p03344 (C++)

Buggy submission (status: Runtime Error):
/**
 *    author:  tourist
 *    created: 26.05.2018 15:10:27
 **/
#include <bits/stdc++.h>

using namespace std;

class dsu {
public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  vector<vector<int>> graph(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  vector<int> add(n);
  iota(add.begin(), add.end(), 0);
  sort(add.begin(), add.end(), [&](int i, int j) {
    return a[add[i]] - b[add[i]] < a[add[j]] - b[add[j]];
  });
  dsu d(n);
  vector<long long> sum_b(n);
  vector<long long> ans(n);
  for (int i = 0; i < n; i++) {
    sum_b[i] = b[i];
    ans[i] = max(a[i], b[i]);
  }
  vector<int> was(n, 0);
  for (int i : add) {
    set<int> s;
    for (int j : graph[i]) {
      if (was[j]) {
        s.insert(d.get(j));
      }
    }
    for (int j : s) {
      sum_b[i] += sum_b[j];
      d.unite(j, i);
    }
    if (!s.empty()) {
      long long best = (long long)1e18;
      for (int j : s) {
        long long sum_b_without = sum_b[i] - sum_b[j];
        long long cur =
            max(ans[j] + sum_b_without, a[i] + sum_b_without - b[i]);
        best = min(best, cur);
      }
      ans[i] = best;
    }
    was[i] = 1;
  }
  cout << ans[d.get(0)] << '\n';
  return 0;
}


Fixed submission:
/**
 *    author:  tourist
 *    created: 26.05.2018 15:10:27
 **/
#include <bits/stdc++.h>

using namespace std;

class dsu {
public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  vector<vector<int>> graph(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  vector<int> add(n);
  iota(add.begin(), add.end(), 0);
  sort(add.begin(), add.end(),
       [&](int i, int j) { return a[i] - b[i] < a[j] - b[j]; });
  dsu d(n);
  vector<long long> sum_b(n);
  vector<long long> ans(n);
  for (int i = 0; i < n; i++) {
    sum_b[i] = b[i];
    ans[i] = max(a[i], b[i]);
  }
  vector<int> was(n, 0);
  for (int i : add) {
    set<int> s;
    for (int j : graph[i]) {
      if (was[j]) {
        s.insert(d.get(j));
      }
    }
    for (int j : s) {
      sum_b[i] += sum_b[j];
      d.unite(j, i);
    }
    if (!s.empty()) {
      long long best = (long long)1e18;
      for (int j : s) {
        long long sum_b_without = sum_b[i] - sum_b[j];
        long long cur =
            max(ans[j] + sum_b_without, a[i] + sum_b_without - b[i]);
        best = min(best, cur);
      }
      ans[i] = best;
    }
    was[i] = 1;
  }
  cout << ans[d.get(0)] << '\n';
  return 0;
}