Problem p03344 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define ivorysi
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define mo 974711
#define MAXN 200005
#define pii pair<int, int>
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, M;
struct node {
  int to, next;
} E[MAXN * 2];
int head[MAXN], sumE, id[MAXN], ra[MAXN], f[MAXN];
int64 A[MAXN], B[MAXN], C[MAXN], dp[MAXN], siz[MAXN];
vector<int> son[MAXN];
int getfa(int x) { return f[x] == x ? x : f[x] = getfa(f[x]); }
bool cmp(int a, int b) { return C[a] <= C[b]; }
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void Init() {
  read(N);
  read(M);
  for (int i = 1; i <= N; ++i) {
    read(A[i]);
    read(B[i]);
    C[i] = max(A[i] - B[i], 0LL);
    id[i] = i;
  }
  sort(id + 1, id + N + 1, cmp);
  for (int i = 1; i <= N; ++i)
    ra[id[i]] = i;
  int u, v;
  for (int i = 1; i <= M; ++i) {
    read(u);
    read(v);
    add(u, v);
    add(v, u);
  }
}
void dfs1(int u) {
  siz[u] += B[u];
  for (auto v : son[u]) {
    dfs1(v);
    siz[u] += siz[v];
  }
}
void dfs2(int u) {
  if (son[u].size() == 0) {
    dp[u] = B[u] + C[u];
    return;
  }
  dp[u] = 1e18;
  for (auto v : son[u]) {
    dfs2(v);
    dp[u] = min(siz[u] - siz[v] + max(C[u], dp[v]), dp[u]);
  }
}
void Solve() {
  for (int i = 1; i <= N; ++i)
    f[i] = i;
  for (int i = 1; i <= N; ++i) {
    int u = id[i];
    for (int j = head[u]; j; j = E[j].next) {
      int v = E[j].to;
      if (ra[getfa(v)] < ra[u] && getfa(v) != u) {
        son[u].pb(getfa(v));
        f[getfa(v)] = u;
      }
    }
  }
  dfs1(id[N]);
  dfs2(id[N]);
  out(dp[id[N]]);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Init();
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #define ivorysi
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define mo 974711
#define MAXN 200005
#define pii pair<int, int>
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int N, M;
struct node {
  int to, next;
} E[MAXN * 2];
int head[MAXN], sumE, id[MAXN], ra[MAXN], f[MAXN];
int64 A[MAXN], B[MAXN], C[MAXN], dp[MAXN], siz[MAXN];
vector<int> son[MAXN];
int getfa(int x) { return f[x] == x ? x : f[x] = getfa(f[x]); }
bool cmp(int s, int t) { return C[s] < C[t]; }
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void Init() {
  read(N);
  read(M);
  for (int i = 1; i <= N; ++i) {
    read(A[i]);
    read(B[i]);
    C[i] = max(A[i] - B[i], 0LL);
    id[i] = i;
  }
  sort(id + 1, id + N + 1, cmp);
  for (int i = 1; i <= N; ++i)
    ra[id[i]] = i;
  int u, v;
  for (int i = 1; i <= M; ++i) {
    read(u);
    read(v);
    add(u, v);
    add(v, u);
  }
}
void dfs1(int u) {
  siz[u] += B[u];
  for (auto v : son[u]) {
    dfs1(v);
    siz[u] += siz[v];
  }
}
void dfs2(int u) {
  if (son[u].size() == 0) {
    dp[u] = B[u] + C[u];
    return;
  }
  dp[u] = 1e18;
  for (auto v : son[u]) {
    dfs2(v);
    dp[u] = min(siz[u] - siz[v] + max(C[u], dp[v]), dp[u]);
  }
}
void Solve() {
  for (int i = 1; i <= N; ++i)
    f[i] = i;
  for (int i = 1; i <= N; ++i) {
    int u = id[i];
    for (int j = head[u]; j; j = E[j].next) {
      int v = E[j].to;
      if (ra[getfa(v)] < ra[u] && getfa(v) != u) {
        son[u].pb(getfa(v));
        f[getfa(v)] = u;
      }
    }
  }
  dfs1(id[N]);
  dfs2(id[N]);
  out(dp[id[N]]);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Init();
  Solve();
  return 0;
}