Problem p00909 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

int N, M;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    vector<int> W(N), i2g(N);
    vector<vector<int>> g2i(N);
    // init
    for (int i = 0; i < N; i++) {
      i2g[i] = i;
      g2i[i].push_back(i);
    }
    while (M--) {
      char c;
      cin >> c;
      if (c == '!') {
        int a, b, w;
        cin >> a >> b >> w;
        a--;
        b--;
        if (i2g[a] == i2g[b]) {
          assert(W[b] - W[a] == w);
        } else {
          // merge
          if (g2i[i2g[a]].size() < g2i[i2g[b]].size()) {
            swap(a, b);
            w = -w;
          }
          int ga = i2g[a], gb = i2g[b];
          int diff = (W[a] + w) - W[b];
          for (int el : g2i[b]) {
            i2g[el] = ga;
            W[el] += diff;
          }
          g2i[ga].insert(g2i[ga].end(), g2i[gb].begin(), g2i[gb].end());
          g2i[gb].clear();
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (i2g[a] != i2g[b]) {
          cout << "UNKNOWN" << endl;
        } else {
          cout << W[b] - W[a] << endl;
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

int N, M;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    vector<int> W(N), i2g(N);
    vector<vector<int>> g2i(N);
    // init
    for (int i = 0; i < N; i++) {
      i2g[i] = i;
      g2i[i].push_back(i);
    }
    while (M--) {
      char c;
      cin >> c;
      if (c == '!') {
        int a, b, w;
        cin >> a >> b >> w;
        a--;
        b--;
        if (i2g[a] == i2g[b]) {
          assert(W[b] - W[a] == w);
        } else {
          // merge
          if (g2i[i2g[a]].size() < g2i[i2g[b]].size()) {
            swap(a, b);
            w = -w;
          }
          int ga = i2g[a], gb = i2g[b];
          int diff = (W[a] + w) - W[b];
          for (int el : g2i[gb]) {
            i2g[el] = ga;
            W[el] += diff;
          }
          g2i[ga].insert(g2i[ga].end(), g2i[gb].begin(), g2i[gb].end());
          g2i[gb].clear();
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (i2g[a] != i2g[b]) {
          cout << "UNKNOWN" << endl;
        } else {
          cout << W[b] - W[a] << endl;
        }
      }
    }
  }
  return 0;
}