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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

class UnionFind {
public:
  const static int MAX_N = 100010;
  int par[MAX_N];
  int s[MAX_N];
  UnionFind() { init(); }
  UnionFind(int n) { init(n); }
  void init() {
    for (int i = 0; i < MAX_N; ++i)
      par[i] = i, s[i] = 1;
  }
  void init(int n) {
    for (int i = 0; i < n; ++i)
      par[i] = i, s[i] = 1;
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (s[x] < s[y])
      par[x] = y, s[y] = s[x] + s[y];
    else
      par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
UnionFind uf;

vector<PII> g[100010];
int d[100010];
void dfs(int x, int p, int dist) {
  d[x] = dist;
  for (auto e : g[x]) {
    if (e.first != p)
      dfs(e.first, x, dist + e.second);
  }
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n)
      break;

    REP(i, n) g[i].clear();
    memset(d, 0, sizeof(d));
    uf.init(n);

    vector<PII> query;
    VI ans(m + 5);
    int idx = 0;
    REP(i, m) {
      char c;
      int a, b;
      cin >> c >> a >> b;
      a--, b--;
      if (c == '!') {
        int w;
        cin >> w;
        if (!uf.same(a, b)) {
          uf.unite(a, b);
          g[a].PB({b, -w});
          g[b].PB({a, w});
        }
      } else {
        query.PB({a, b});
        if (!uf.same(a, b))
          ans[idx] = LLINF;
        idx++;
      }
    }

    // cout << query << endl;
    REP(i, n) {
      if (d[i] == 0)
        dfs(i, -1, 0);
      // cout << "i:" << i << endl;
    }
    // REP(i, n) cout << d[i] << " ";
    // cout << endl;

    REP(i, query.size()) {
      if (ans[i] == LLINF)
        cout << "UNKNOWN" << endl;
      else
        cout << d[query[i].first] - d[query[i].second] << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

class UnionFind {
public:
  const static int MAX_N = 100010;
  int par[MAX_N];
  int s[MAX_N];
  UnionFind() { init(); }
  UnionFind(int n) { init(n); }
  void init() {
    for (int i = 0; i < MAX_N; ++i)
      par[i] = i, s[i] = 1;
  }
  void init(int n) {
    for (int i = 0; i < n; ++i)
      par[i] = i, s[i] = 1;
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (s[x] < s[y])
      par[x] = y, s[y] = s[x] + s[y];
    else
      par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
UnionFind uf;

vector<PII> g[100010];
int d[100010];
void dfs(int x, int p, int dist) {
  d[x] = dist;
  for (auto e : g[x]) {
    if (e.first != p)
      dfs(e.first, x, dist + e.second);
  }
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n, m;
    cin >> n >> m;
    if (!n)
      break;

    REP(i, n) g[i].clear();
    memset(d, 0, sizeof(d));

    vector<PII> query;
    VI ans(m + 5);
    int idx = 0;
    REP(i, m) {
      char c;
      int a, b;
      cin >> c >> a >> b;
      a--, b--;
      if (c == '!') {
        int w;
        cin >> w;
        uf.unite(a, b);
        g[a].PB({b, -w});
        g[b].PB({a, w});
      } else {
        query.PB({a, b});
        if (!uf.same(a, b))
          ans[idx] = LLINF;
        idx++;
      }
    }
    dfs(0, -1, 0);

    REP(i, query.size()) {
      if (ans[i] == LLINF)
        cout << "UNKNOWN" << endl;
      else
        cout << d[query[i].first] - d[query[i].second] << endl;
    }
  }

  return 0;
}