alexjercan/bugnet huggingface dataset:
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

vector<pair<int, int>> UFtree(100010);
vector<int> treeSize;

int root_Diff(int i) {
  if (UFtree[i].first == i)
    return 0;
  else
    return UFtree[i].second + root_Diff(UFtree[i].first);
}

int root(int i) {
  if (UFtree[i].first == i)
    return i;
  else {
    UFtree[i].second = root_Diff(i);
    return (UFtree[i].first = root(UFtree[i].first));
  }
}

bool is_same_set(int i, int j) { return root(i) == root(j); }

void unite(int i, int j, int w) { // w_i-w_j = w
  int x, y, tmp1, tmp2;
  x = root(i);
  y = root(j);
  tmp1 = treeSize[x];
  tmp2 = treeSize[y];
  if (tmp1 > tmp2) {
    UFtree[y].first = x;
    UFtree[y].second = w + UFtree[i].second - UFtree[j].second;
  } else {
    UFtree[x].first = y;
    UFtree[x].second = -w - UFtree[i].second + UFtree[j].second;
  }
  if (x != i) {
    treeSize[i] += tmp2;
    treeSize[x] += tmp2;
  } else
    treeSize[i] += tmp2;
  if (y != j) {
    treeSize[y] += tmp1;
    treeSize[j] += tmp1;
  } else
    treeSize[j] += tmp1;
}

int main() {
  int N, M;
  vector<int> c(100010, 1);
  while (cin >> N >> M) {
    if (!N)
      break;
    for (int i = 1; i <= N; i++) {
      UFtree[i] = make_pair(i, 0);
    }
    treeSize = c;
    string word;
    int a, b, w;
    for (int i = 0; i < M; i++) {
      cin >> word >> a >> b;
      if (word == "!") {
        cin >> w;
        unite(b, a, w);
      } else {
        if (is_same_set(a, b))
          cout << root_Diff(a) - root_Diff(b) << endl;
        else
          cout << "UNKNOWN" << endl;
      }
    }
    UFtree.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

vector<pair<int, int>> UFtree(100010);

int root_Diff(int i) {
  if (UFtree[i].first == i)
    return 0;
  else
    return UFtree[i].second + root_Diff(UFtree[i].first);
}

int root(int i) {
  if (UFtree[i].first == i)
    return i;
  else {
    UFtree[i].second = root_Diff(i);
    return (UFtree[i].first = root(UFtree[i].first));
  }
}

bool is_same_set(int i, int j) { return root(i) == root(j); }

void unite(int i, int j, int w) { // w_i-w_j = w
  int x, y;
  x = root(i);
  y = root(j);
  UFtree[y].first = x;
  UFtree[y].second = w + UFtree[i].second - UFtree[j].second;
}

int main() {
  int N, M;
  while (cin >> N >> M) {
    if (!N)
      break;
    for (int i = 1; i <= N; i++) {
      UFtree[i] = make_pair(i, 0);
    }
    string word;
    int a, b, w;
    for (int i = 0; i < M; i++) {
      cin >> word >> a >> b;
      if (word == "!") {
        cin >> w;
        unite(b, a, w);
      } else {
        if (is_same_set(a, b))
          cout << root_Diff(a) - root_Diff(b) << endl;
        else
          cout << "UNKNOWN" << endl;
      }
    }
    UFtree.clear();
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
const int N = 110000;
int fa[N];
int fd(int x) {
  if (fa[x] == -1)
    return x;
  return fa[x] = fd(fa[x]);
}
ll w[N];
bool ss[N];
int a[N], b[N], od[N];
vector<pair<int, ll>> bj[N];
void dfs(int st, ll wt) {
  w[st] = wt;
  ss[st] = true;
  vector<pair<int, ll>>::iterator pa;
  for (pa = bj[st].begin(); pa != bj[st].end(); pa++) {
    int tt = pa->first;
    ll ad = pa->second;
    if (!ss[tt])
      dfs(tt, ad + wt);
  }
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      fa[i] = -1;
      bj[i].clear();
      ss[i] = false;
    }
    for (int i = 0; i < m; i++) {
      char s[5];
      scanf("%s%d%d", s, &a[i], &b[i]);
      if (s[0] == '!') {
        ll c;
        od[i] = 0;
        scanf("%lld", &c);
        // cd[b[i]] ++;
        bj[a[i]].push_back(mp(b[i], c));
        bj[b[i]].push_back(mp(a[i], -c));
      } else
        od[i] = 1;
    }
    // memset(w, -1, sizeof w);
    for (int i = 1; i <= n; i++) {
      if (!ss[i]) {
        dfs(i, 0);
      }
    }
    // for ( int i = 1; i <= n; i ++ ) printf("%d\\n", w[i]);
    for (int i = 0; i < m; i++) {
      if (od[i] == 0) {
        int sa = fd(a[i]), sb = fd(b[i]);
        if (sa != sb)
          fa[sa] = sb;
      } else {
        int sa = fd(a[i]), sb = fd(b[i]);
        if (sa == sb)
          printf("%lld\\n", w[b[i]] - w[a[i]]);
        else
          puts("UNKNOWN");
      }
    }
  }
  return 0;
}
/*
3 4
! 1 3 5
! 1 2 2
? 1 2
? 1 3
*/

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define N 100010
#define cl(xx, yy) memset((xx), (yy), sizeof((xx)))

int n;
int p[N], val[N];

int find(int x) {
  int fa = p[x];
  if (x == fa)
    return x;
  int rt = find(p[x]);
  val[x] += val[fa];
  p[x] = rt;
  return rt;
}

void updata(int a, int b, int w) {
  int rtb = find(b);
  int delta = w - val[b];
  p[rtb] = a;
  val[rtb] += delta;
}

int main() {

  // freopen("input.txt","r",stdin);

  int m;
  while (scanf("%d%d", &n, &m) != EOF) {
    if (!n && !m)
      break;
    for (int i = 1; i <= n; i++) {
      val[i] = 0;
      p[i] = i;
    }
    while (m--) {
      char op[10];
      int a, b, w;
      scanf("%s", op);
      if (op[0] == '!') {
        scanf("%d%d%d", &a, &b, &w);
        updata(a, b, w);
      } else {
        scanf("%d%d", &a, &b);
        int rta = find(a);
        int rtb = find(b);
        if (rta != rtb)
          puts("UNKNOWN");
        else
          printf("%d\\n", val[b] - val[a]);
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int M = 100005;
int n, m, x, y, tmp;
long long p[M];
int fa[M];
char c[5];

void init() {
  for (int i = 0; i < M; i++)
    fa[i] = i;
  memset(p, 0, sizeof(p));
}

int getfa(int x) {
  if (fa[x] == x)
    return fa[x];
  else {
    int root = getfa(fa[x]);
    p[x] += p[fa[x]];
    return fa[x] = root;
  }
}

int main() {
  while (scanf("%d %d", &n, &m), n) {
    init();
    while (m--) {
      scanf("%s %d %d", c, &x, &y);
      if (c[0] == '!') {
        scanf("%d", &tmp);
        int xx = getfa(x);
        int yy = getfa(y);
        if (xx != yy) {
          int tmp1 = p[x] - p[xx];
          int tmp2 = p[y] - p[yy];
          fa[xx] = yy;
          p[xx] += tmp2 - tmp - tmp1;
        }
      } else {
        int xx = getfa(x);
        int yy = getfa(y);
        if (xx == yy)
          printf("%lld\\n", p[y] - p[x]);
        else
          puts("UNKNOWN");
      }
      // for(int i=1;i<=4;i++) cout<<p[i]<<' ';cout<<endl;
    }
  }
  return 0;
}

/*
4 7
! 1 2 100
! 2 3 100
! 4 3 150
? 4 1
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;
const int top = 100000 + 100;

int w[top];
int father[top];
int dis[top], vis[top], head[top];
int n;
int Find(int a, int &cost) {
  int ta = a;
  while (a != father[a]) {
    cost += dis[a];
    a = father[a];
  }
  father[ta] = a;
  dis[ta] = cost;
  return a;
}

void Merge(int _a, int _b, int _cost) {
  int c1 = 0, c2 = 0;
  int a = Find(_a, c1), b = Find(_b, c2);
  // printf("In Function
  // Merge,a=%d,b=%d,c1=%d,c2=%d,_cost=%d\\n",a,b,c1,c2,_cost);
  if (a < b) {
    father[b] = a;
    dis[b] = c1 - c2 + _cost;
  }

  else {
    father[a] = b;
    dis[a] = -c1 + c2 - _cost;
  }
  //    printf("In Function
  //    Merge,a=%d,b=%d,c1=%d,c2=%d,_cost=%d\\n",a,b,c1,c2,_cost);
  // printf("dis[a]=%d,dis[b]=%d\\n",dis[a],dis[b]);
}

int main() {
  int m;
  int a, b, d;
  int i;
  char order[2];
  while (scanf("%d%d", &n, &m) && n + m) {
    memset(w, INF, sizeof(w));
    memset(head, -1, sizeof(head));
    //   cnt=0;
    for (i = 1; i <= n; i++) {
      father[i] = i;
    }
    for (i = 0; i < m; i++) {
      scanf("%s", order);
      if (order[0] == '!') {
        scanf("%d%d%d", &a, &b, &d);
        Merge(a, b, d);
      } else {
        scanf("%d%d", &a, &b);
        int c1 = 0, c2 = 0;
        int fa = Find(a, c1), fb = Find(b, c2);
        if (fa != fb) {
          printf("UNKNOWN\\n");
        } else {
          //  printf("c1=%d,c2=%d\\n",c1,c2);
          printf("%d\\n", c2 - c1);
        }
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
#define MAX 1000010
using namespace std;

long long p[MAX], w[MAX];

pair<int, int> find(int a) {
  if (p[a] == a)
    return mp(a, 0);
  pair<int, int> pii = find(p[a]);
  p[a] = pii.f;
  w[a] += pii.s;
  return mp(p[a], w[a]);
}

void unit(int a, int b, int d) {
  pair<int, int> pa = find(a), pb = find(b);
  if (pa.f == pb.f)
    return;
  d -= w[b] - w[a];
  p[pa.f] = pb.f;
  w[pa.f] = -d;
}

int main() {
  int n, q, a, b, d;
  char c;

  while (cin >> n >> q && n) {

    for (int i = 0; i < n; i++) {
      p[i] = i;
      w[i] = 0;
    }

    for (int i = 0; i < q; i++) {
      cin >> c;
      if (c == '!') {
        cin >> a >> b >> d;
        a--;
        b--;
        unit(a, b, d);
      } else {
        cin >> a >> b;
        a--;
        b--;
        pair<int, int> pa = find(a), pb = find(b);
        if (pa.f != pb.f)
          cout << "UNKNOWN" << endl;
        else
          cout << w[b] - w[a] << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
#define MAX 1000010
using namespace std;

long long p[MAX], w[MAX];

pair<int, int> find(int a) {
  if (p[a] == a)
    return mp(a, 0);
  pair<int, int> pii = find(p[a]);
  pii.s += w[a];
  p[a] = pii.f;
  w[a] = pii.s;
  return pii;
}

void unit(int a, int b, int d) {
  pair<int, int> pa = find(a), pb = find(b);
  // if(pa.f==pb.f)return;
  p[pa.f] = b;
  w[pa.f] = d - pa.s + pb.s;
}

int main() {
  int n, q, a, b, d;
  char c;

  while (cin >> n >> q && n) {

    for (int i = 0; i < n; i++) {
      p[i] = i;
      w[i] = 0;
    }

    for (int i = 0; i < q; i++) {
      cin >> c;
      if (c == '!') {
        cin >> a >> b >> d;
        a--;
        b--;
        unit(a, b, d);
      } else {
        cin >> a >> b;
        a--;
        b--;
        pair<int, int> pa = find(a), pb = find(b);
        if (pa.f != pb.f)
          cout << "UNKNOWM" << endl;
        else
          cout << pa.s - pb.s << endl;
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_N 100005
using namespace std;
typedef pair<int, int> P;
P par[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = P(i, 0);
}

P find(int x) {
  if (par[x].first == x)
    return P(x, 0);
  P r = find(par[x].first);
  return par[x] = P(r.first, par[x].second + r.second);
}

void unite(int x, int y, int cost) {
  P r1 = find(x);
  P r2 = find(y);
  if (r1.first == r2.first)
    return;
  par[r1.first] = P(r2.first, r2.second - r1.second + cost);
}

bool same(int x, int y) { return find(x).first == find(y).first; }

int n, m, a, b, cost;
char c;

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    init(n);
    rep(i, m) {
      scanf("%c", &c);
      scanf("%c%d%d", &c, &a, &b);
      if (c == '!') {
        scanf("%d", &cost);
        unite(b - 1, a - 1, cost);
      } else {
        if (!same(a - 1, b - 1))
          printf("UNKNOWN\\n");
        else
          printf("%d\\n", find(b - 1).second - find(a - 1).second);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_N 100005
using namespace std;
typedef pair<int, int> P;
P par[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = P(i, 0);
}

P find(int x) {
  if (par[x].first == x)
    return P(x, 0);
  P r = find(par[x].first);
  return par[x] = P(r.first, par[x].second + r.second);
}

void unite(int x, int y, int cost) {
  P r1 = find(x);
  P r2 = find(y);
  if (r1.first == r2.first)
    return;
  par[r1.first] = P(r2.first, r2.second - r1.second + cost);
}

bool same(int x, int y) { return find(x).first == find(y).first; }

int n, m, a, b, cost;
char c;

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    init(n);
    rep(i, m) {
      scanf("%c%d%d", &c, &a, &b);
      if (c == '!') {
        scanf("%d", &cost);
        unite(b - 1, a - 1, cost);
      } else {
        if (!same(a - 1, b - 1))
          printf("UNKNOWN\\n");
        else
          printf("%d\\n", find(b - 1).second - find(a - 1).second);
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 3000000
using namespace std;
typedef long long ll;
struct P {
  ll color, w;
  P(ll color = -inf, ll w = -inf) : color(color), w(w) {}
};
P par[MAX];
void init() { rep(i, MAX) par[i] = P(i); }

P find(int x) {
  if (x == par[x].color)
    return par[x];
  P p = find(par[x].color);
  if (par[x].w == -inf)
    par[x].w = 0;
  par[x].w += p.w, par[x].color = p.color;
  return par[x];
}

void unit(int x, int y, ll w) // x <= y
{
  P X = find(x);
  P Y = find(y);
  // if(X.color == Y.color)return;
  par[X.color].color = par[Y.color].color;
  if (par[X.color].w == -inf)
    par[X.color].w = 0;
  if (par[Y.color].w == -inf)
    par[Y.color].w = 0;
  par[X.color].w = par[X.color].w + (par[y].w - w - par[x].w);
  // par[x].w -= par[y].w;
}

int N, M;

int main() {
  while (cin >> N >> M, N | M) {
    init();
    char c;
    ll a, b, w;
    rep(_, M) {
      cin >> c;
      if (c == '!') {
        cin >> a >> b >> w;
        unit(a, b, w);
      } else {
        cin >> a >> b;
        P B = find(b);
        P A = find(a);
        if (B.w == -inf || A.w == -inf)
          cout << "UNKNOWN" << endl;
        else if (B.color != A.color)
          cout << "UNKNOWN" << endl;
        else
          cout << B.w - A.w << endl;
      }

      rep(i, 10) {
        // cout << i << " = " << par[i].color << "," << par[i].w << endl;
      }
      // cout << endl;
    }
  }
  return 0;
}

Error: -6
7070f984-c387-4bb9-887a-f1fa3056441b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00909/C++/s537918511.cpp:43: int main(): Assertion `false' failed.


Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 28)
#define MAX 1000000
using namespace std;
struct P {
  int color, w;
  P(int color = -inf, int w = -inf) : color(color), w(w) {}
};
P par[MAX];
void init() { rep(i, MAX) par[i] = P(i); }

P find(int x) {
  if (x == par[x].color)
    return par[x];
  P p = find(par[x].color);
  if (par[x].color == -inf)
    par[x].color = 0;
  par[x].w += p.w, par[x].color = p.color;
  return par[x];
}

void unit(int x, int y, int w) // x <= y
{
  P X = find(x);
  P Y = find(y);
  assert(X.color != Y.color);
  par[X.color].color = par[Y.color].color;
  if (par[X.color].w == -inf)
    par[X.color].w = 0;
  if (par[Y.color].w == -inf)
    par[Y.color].w = 0;
  par[X.color].w = par[X.color].w - w + (Y.w != -inf ? Y.w : 0);
}

int N, M;

int main() {
  while (cin >> N >> M, N | M) {
    assert(false);
    init();
    char c;
    int a, b, w;
    rep(_, M) {
      cin >> c;
      if (c == '!') {
        cin >> a >> b >> w;
        unit(a, b, w);
      } else {
        cin >> a >> b;
        int B = find(b).w;
        int A = find(a).w;
        if (B == -inf || A == -inf)
          cout << "UNKNOWN" << endl;
        else
          cout << B - A << endl;
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

typedef pair<int, int> pii;

#define x first
#define y second

class UF {
public:
  vector<pii> per;
  void init(int n) {
    per = vector<pii>(n + 1);
    rep(i, per.size()) {
      per[i].x = i;
      per[i].y = 0;
    }
  }

  int find(int a) {
    if (per[a].x == a)
      return a;

    int ret = find(per[a].x);
    per[a].y += per[per[a].x].y;
    per[a].x = ret;
    return ret;
  }

  void uni(int a, int b, int dist) {
    int s = find(a);
    int t = find(b);

    if (s == t)
      return; // assert(0);

    int c = per[b].y - per[a].y;
    per[t].x = s;
    per[t].y = -(c + dist);
  }

  bool same(int a, int b) { return find(a) == find(b); }

  void print() {
    reps(i, 1, per.size()) printf("(%d,%d) ", per[i].x, per[i].y);
    puts("");
  }
};

UF uf;
int n, m;

bool input() {
  cin >> n >> m;
  return n != 0;
}

void uni(int a, int b, int c) { uf.uni(a, b, c); }

string itostr(int n) {
  stringstream s;
  s << n;
  return s.str();
}

string compare(int a, int b) {
  if (!uf.same(a, b))
    return "UNKNOWN";
  int ans = -(uf.per[b].y - uf.per[a].y);
  return itostr(ans);
}

void solve() {
  uf.init(n);

  rep(i, m) {
    char ch;
    int a, b, c;

    cin >> ch;
    if (ch == '!') {
      cin >> a >> b >> c;
      uni(a, b, c);
    } else {
      cin >> a >> b;
      cout << compare(a, b) << endl;
    }
    // uf.print();
  }
}

int main() {
  while (input())
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

typedef pair<int, int> pii;

#define x first
#define y second

class UF {
public:
  vector<pii> per;
  void init(int n) {
    per = vector<pii>(n + 1);
    rep(i, per.size()) {
      per[i].x = i;
      per[i].y = 0;
    }
  }

  int find(int a) {
    if (per[a].x == a)
      return a;

    int ret = find(per[a].x);
    per[a].y += per[per[a].x].y;
    per[a].x = ret;
    return ret;
  }

  int uni(int a, int b, int dist) {
    int s = find(a);
    int t = find(b);

    if (s == t)
      assert(0);

    int c = per[b].y - per[a].y;
    per[t].x = s;
    per[t].y = -(c + dist);
  }

  bool same(int a, int b) { return find(a) == find(b); }

  void print() {
    reps(i, 1, per.size()) printf("%d,%d ", per[i].x, per[i].y);
    puts("");
  }
};

UF uf;
int n, m;

bool input() {
  cin >> n >> m;
  return n != 0;
}

void uni(int a, int b, int c) { uf.uni(a, b, c); }

string itostr(int n) {
  stringstream s;
  s << n;
  return s.str();
}

string compare(int a, int b) {
  if (!uf.same(a, b))
    return "UNKNOWN";
  int ans = -(uf.per[b].y - uf.per[a].y);
  return itostr(ans);
}

void solve() {
  uf.init(n);

  rep(i, m) {
    char ch;
    int a, b, c;

    cin >> ch;
    if (ch == '!') {
      cin >> a >> b >> c;
      uni(a, b, c);
    } else {
      cin >> a >> b;
      cout << compare(a, b) << endl;
    }
  }
}

int main() {
  while (input())
    solve();
}
Problem p00909 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
long long dist[1 << 18];
long long group[1 << 18];
char q;
long long n, Q, a, b, c;
vector<long long> gr[1 << 18];
int main() {
  while (true) {
    for (int i = 0; i < (1 << 18); i++) {
      dist[i] = 0;
      group[i] = i;
      gr[i].clear();
      gr[i].push_back(i);
    }
    cin >> n >> Q;
    if (n == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      cin >> q;
      if (q == '?') {
        cin >> a >> b;
        if (group[a] != group[b])
          cout << "UNKNOWN" << endl;
        else
          cout << dist[b] - dist[a] << endl;
      }
      if (q == '!') {
        cin >> a >> b >> c;
        long long L = group[a], M = group[b];
        if (group[a] != group[b] && gr[L].size() >= gr[M].size()) {
          long long K = dist[a] + c - dist[b];
          for (int j = 0; j < gr[M].size(); j++) {
            group[gr[M][j]] = L;
            gr[L].push_back(gr[M][j]);
            dist[gr[M][j]] += K;
          }
          gr[M].clear();
        } else if (group[a] != group[b]) {
          long long K = dist[b] - c - dist[a];
          for (int j = 0; j < gr[L].size(); j++) {
            group[gr[L][j]] = M;
            gr[M].push_back(gr[L][j]);
            dist[gr[L][j]] += K;
          }
          gr[L].clear();
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
long long dist[1 << 18];
long long group[1 << 18];
char q;
long long n, Q, a, b, c;
vector<long long> gr[1 << 18];
int main() {
  while (true) {
    for (int i = 0; i < (1 << 18); i++) {
      dist[i] = 0;
      group[i] = i;
      gr[i].clear();
      gr[i].push_back(i);
    }
    cin >> n >> Q;
    if (n == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      cin >> q;
      if (q == '?') {
        cin >> a >> b;
        if (group[a] != group[b])
          cout << "UNKNOWN" << endl;
        else
          cout << dist[b] - dist[a] << endl;
      }
      if (q == '!') {
        cin >> a >> b >> c;
        long long L = group[a], M = group[b];
        if (group[a] != group[b] && gr[L].size() <= gr[M].size()) {
          long long K = dist[a] + c - dist[b];
          for (int j = 0; j < gr[M].size(); j++) {
            group[gr[M][j]] = L;
            gr[L].push_back(gr[M][j]);
            dist[gr[M][j]] += K;
          }
          gr[M].clear();
        } else if (group[a] != group[b]) {
          long long K = dist[b] - c - dist[a];
          for (int j = 0; j < gr[L].size(); j++) {
            group[gr[L][j]] = M;
            gr[M].push_back(gr[L][j]);
            dist[gr[L][j]] += K;
          }
          gr[L].clear();
        }
      }
    }
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
pair<int, long long> par[100000];
pair<int, long long> root(int x) {
  if (par[x].first == x)
    return par[x];
  pair<int, long long> ret = root(par[x].first);
  return par[x] = make_pair(ret.first, ret.second + par[x].second);
}
bool same(int x, int y) { return root(x).first == root(y).first; }
void unite(int x, int y, int w) {
  pair<int, long long> tx = root(x);
  pair<int, long long> ty = root(y);
  if (tx.first != ty.first) {
    par[tx.first] = make_pair(ty.first, ty.second - tx.second + w);
  }
}
long long query(int a, int b) { return root(a).second - root(b).second; }
int N, Q, a, b, w;
char c;
int main() {
  while (true) {
    scanf("%d%d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    for (int i = 0; i < N; i++)
      par[i] = make_pair(i, 0);
    for (int i = 0; i < Q; i++) {
      cin >> c;
      if (c == '!') {
        scanf("%d%d%d", &a, &b, &w);
        unite(a - 1, b - 1, w);
      } else {
        scanf("%d%d", &a, &b);
        if (!same(a - 1, b - 1)) {
          printf("UNKNOWN\\n");
        } else {
          printf("%lld\\n", query(a - 1, b - 1));
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
int par[100000], dat[100000];
int root(int x) { return par[x] == x ? x : root(par[x]); }
int dist(int x) { return par[x] == x ? 0 : dist(par[x]) + dat[x]; }
int same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y, int w) {
  x = root(x);
  y = root(y);
  if (x != y) {
    par[x] = y;
    dat[x] = w;
  }
}
int query(int a, int b) { return dist(a) - dist(b); }
int N, Q, a, b, w;
char c;
int main() {
  while (true) {
    scanf("%d%d", &N, &Q);
    if (N == 0 && Q == 0)
      break;
    for (int i = 0; i < N; i++)
      par[i] = i, dat[i] = 0;
    for (int i = 0; i < Q; i++) {
      cin >> c;
      if (c == '!') {
        scanf("%d%d%d", &a, &b, &w);
        unite(a - 1, b - 1, w);
      } else {
        scanf("%d%d", &a, &b);
        if (!same(a - 1, b - 1)) {
          printf("UNKNOWN\\n");
        } else {
          printf("%d\\n", query(a - 1, b - 1));
        }
      }
    }
  }
  return 0;
}