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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<int, int> P;
struct UF {
  vector<int> data;
  vector<int> weight;
  UF(int N) : data(N, -1), weight(N, 0) {}
  void uni(int x, int y, int w) {
    P px = root(x), py = root(y);
    if (px.first != py.first) {
      if (data[px.first] > data[px.first]) {
        swap(px, py);
        w *= -1;
      }
      data[px.first] += data[py.first];
      data[py.first] = px.first;
      weight[py.first] = px.second - py.second + w;
    }
  }
  P root(int x) {
    if (data[x] < 0)
      return P(x, 0);
    P p = root(data[x]);
    data[x] = p.first;
    weight[x] += p.second;
    return P(data[x], weight[x]);
  }
};

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    UF uf(N);
    string s;
    int x, y, z;
    REP(i, M) {
      cin >> s;
      if (s == "!") {
        scanf("%d %d %d", &x, &y, &z);
        x--;
        y--;
        uf.uni(x, y, z);
      } else {
        scanf("%d %d", &x, &y);
        x--;
        y--;
        P px = uf.root(x);
        P py = uf.root(y);
        if (px.first == py.first) {
          printf("%d\\n", py.second - px.second);
        } else {
          printf("UNKNOWN\\n");
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<int, int> P;
struct UF {
  vector<int> data;
  vector<int> weight;
  UF(int N) : data(N, -1), weight(N, 0) {}
  void uni(int x, int y, int w) {
    P px = root(x), py = root(y);
    if (px.first != py.first) {
      if (data[px.first] > data[px.first]) {
        swap(px, py);
        w *= -1;
      }
      data[px.first] += data[py.first];
      data[py.first] = px.first;
      weight[py.first] = px.second - py.second + w;
    }
  }
  P root(int x) {
    if (data[x] < 0)
      return P(x, 0);
    P p = root(data[x]);
    data[x] = p.first;
    weight[x] += p.second;
    return P(data[x], weight[x]);
  }
};

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    UF uf(N);
    string s;
    int x, y, z;
    REP(i, M) {
      cin >> s;
      if (s == "!") {
        scanf("%d %d %d", &x, &y, &z);
        uf.uni(x, y, z);
      } else {
        scanf("%d %d", &x, &y);
        P px = uf.root(x);
        P py = uf.root(y);
        if (px.first == py.first) {
          printf("%d\\n", py.second - px.second);
        } else {
          printf("UNKNOWN\\n");
        }
      }
    }
  }
  return 0;
}