Problem p03344 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using db = double;
using vi = vector<int>;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << #x << '	' << x << endl
const int inf = 0x3f3f3f3f;
const db eps = 1e-8;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

const int maxn = 200010;
int n, m;
ll a[maxn], b[maxn], sum[maxn], f[maxn], p[maxn], dp[maxn], vis[maxn];
int Find(int x) { return f[x] == x ? x : Find(f[x]); }
vector<int> G[maxn];
bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
    a[i] = max(a[i] - b[i], 0ll);
    p[i] = f[i] = i;
    sum[i] = b[i];
    dp[i] = a[i];
  }
  for (int i = 0, u, v; i < m; ++i) {
    cin >> u >> v;
    G[v].pb(u);
    G[u].pb(v);
  }
  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; ++i) {
    int u = p[i];
    vis[u] = 1;
    for (auto v : G[u]) {
      if (vis[v]) {
        int x = Find(u), y = Find(v);
        if (x != y) {
          f[y] = x;
          sum[x] += sum[y];
          dp[x] = min(dp[x], max(dp[y], a[x] - sum[y]));
        }
      }
    }
  }
  cout << dp[Find(1)] + sum[Find(1)] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using db = double;
using vi = vector<int>;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << #x << '	' << x << endl
const int inf = 0x3f3f3f3f;
const db eps = 1e-8;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

const int maxn = 200010;
int n, m;
ll a[maxn], b[maxn], sum[maxn], f[maxn], p[maxn], dp[maxn], vis[maxn];
int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); }
vector<int> G[maxn];
bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
    a[i] = max(a[i] - b[i], 0ll);
    p[i] = f[i] = i;
    sum[i] = b[i];
    dp[i] = a[i];
  }
  for (int i = 0, u, v; i < m; ++i) {
    cin >> u >> v;
    G[v].pb(u);
    G[u].pb(v);
  }
  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; ++i) {
    int u = p[i];
    vis[u] = 1;
    for (auto v : G[u]) {
      if (vis[v]) {
        int x = Find(u), y = Find(v);
        if (x != y) {
          f[y] = x;
          sum[x] += sum[y];
          dp[x] = min(dp[x], max(dp[y], a[x] - sum[y]));
        }
      }
    }
  }
  cout << dp[Find(1)] + sum[Find(1)] << "\n";
}