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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 100100
using namespace std;
long long high[maxn], f[maxn], num[maxn]; // f隶ー蠖穂ク贋ク?アゑシ敬igh隶ー蠖穂ク矩擇譛牙?蝮礼??系um隶ー蠖穂サ・i荳コ譬ケ逧??蟄占鰍轤ケ荳ェ謨ー
                                          // (豺ア蠎ヲ??
void init() // 蛻晏ァ句喧
{
  for (int i = 0; i < maxn; i++) {
    f[i] = i;
    high[i] = 0; // 譬ケ闃らせ逧?ォ伜コヲ荳コ0
  }
}
int find(int x) {
  if (x == f[x])
    return f[x];
  int r = f[x];
  f[x] = find(f[x]);
  high[x] += high[r]; // 霍晄?闃らせ逧?キ晉ヲサ
  return f[x];        // x逧??闃らせ
}
void unio(int x, int y, int w) {
  int fx = find(x);
  int fy = find(y);
  if (fx == fy)
    return;
  f[fx] = fy; // fx逧??闃らせ蜿倅クコfy
  high[fx] = high[y] - high[x] - w;
  // cout<<fx<<" "<<fy<<"  "<<high[fx]<<endl;
}
int main() {
  int t, n, m;
  char s[5];
  int a, b, w;
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0) {
      return 0;
    }
    init();
    while (m--) {
      scanf("%s", s);
      if (s[0] == '!') {
        scanf("%d%d%d", &a, &b, &w);
        unio(a, b, w);
      } else {
        scanf("%d%d", &a, &b);
        int fx = find(a);
        int fy = find(b);
        if (fx == fy) {
          printf("%lld\\n", high[b] - high[a]);
        } else {
          puts("UNKNOWN");
        }
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<int> V[100000 + 10];
int main() {
  int n, m;
  char input[100];
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i <= n; i++) {
      V[i].pop_back();
    }
    map<P, int> M;
    for (int i = 0; i < m; i++) {
      scanf("%s", input);
      if (input[0] == '!') {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        P tmpP;
        tmpP.first = a;
        tmpP.second = b;
        V[a].push_back(b);
        V[b].push_back(a);
        M[tmpP] = w;
        tmpP.first = b;
        tmpP.second = a;
        M[tmpP] = -w;
      } else { // ?
        P tmpP;
        scanf("%d%d", &tmpP.first, &tmpP.second);
        if (M.find(tmpP) != M.end()) {
          printf("%d\\n", M[tmpP]);
        } else {
          int st = tmpP.first, ed = tmpP.second;
          queue<P> Q;
          queue<int> COST;
          P OAO;
          OAO.second = st;
          for (int i = 0; i < V[st].size(); i++) {
            OAO.first = V[st][i];
            COST.push(M[OAO]);
            Q.push(OAO);
          }
          int cost = -1;
          while (!Q.empty()) {
            P QAQ = Q.front();
            Q.pop();
            int ccc = COST.front();
            COST.pop();
            if (QAQ.first == ed) {
              cost = ccc;
              break;
            }
            for (int i = 0; i < V[QAQ.first].size(); i++) {
              if (V[QAQ.first][i] != QAQ.second) {
                P ttt;
                ttt.first = V[QAQ.first][i];
                ttt.second = QAQ.first;
                Q.push(ttt);
                COST.push(M[ttt] + ccc);
              }
            }
          }
          if (cost == -1)
            printf("UNKNOWN\\n");
          else
            printf("%d\\n", cost);
        }
      }
    }
  }
  return 0;
}
/*
2 2
! 1 2 1
? 1 2
2 2
! 1 2 1
? 2 1
4 7
! 1 2 100
? 2 3
! 2 3 100
? 2 3
? 1 3
! 4 3 150
? 4 1
0 0
*/