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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
  int T;
  cin >> T;
  while (T--) {
    int p, q;
    cin >> p >> q;
    int P = p * p + q * q;
    int cnt = 0;
    int ma = 0;
    for (int i = 0; i * i <= P; i++)
      ma = i;
    for (int m = -ma; m <= ma; m++) {
      for (int n = -ma; n <= ma; n++) {
        int tmp = m * m + n * n;
        if (tmp == 0)
          continue;
        if (P % tmp == 0) {
          int mp = m * p + n * q;
          int mq = m * q - n * p;
          if (mp % tmp == 0 && mq % tmp == 0)
            cnt++;
        }
      }
    }
    if (cnt == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
  int T;
  cin >> T;
  while (T--) {
    int p, q;
    cin >> p >> q;
    int P = p * p + q * q;
    int cnt = 0;
    int ma = 0;
    for (int i = 0; i <= P; i++)
      ma = i;
    for (int m = -ma; m <= ma; m++) {
      for (int n = -ma; n <= ma; n++) {
        int tmp = m * m + n * n;
        if (tmp == 0)
          continue;
        if (P % tmp == 0) {
          int mp = m * p + n * q;
          int mq = m * q - n * p;
          if (mp % tmp == 0 && mq % tmp == 0)
            cnt++;
        }
      }
    }
    if (cnt == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int cnt[221212];
int X[221212];
int main() {
  int i, j;
  for (i = -200; i <= 200; i++) {
    for (j = i; j <= 200; j++) {
      if (i == 0 || j == 0)
        cnt[i * i + j * j] += 2;
      else
        cnt[i * i + j * j]++;
    }
  }
  for (i = 0; i <= 20000; i++) {
    for (j = 0; i * j <= 20000 && j <= 20000; j++) {
      if (cnt[i] && cnt[j])
        X[i * j] += cnt[i];
    }
  }
  int t, tc;
  scanf("%d", &t);
  for (tc = 0; tc < t; tc++) {
    int a, b;
    scanf("%d%d", &a, &b);
    puts(X[a * a + b * b] != 8 ? "C" : "P");
  }
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>

int cnt[221212];
int X[221212];
int main() {
  int i, j;
  for (i = -400; i <= 400; i++) {
    for (j = i; j <= 400; j++) {
      if (i == 0 || j == 0)
        cnt[i * i + j * j] += 2;
      else
        cnt[i * i + j * j]++;
    }
  }
  for (i = 0; i <= 20000; i++) {
    for (j = 0; i * j <= 20000 && j <= 20000; j++) {
      if (cnt[i] && cnt[j])
        X[i * j] += cnt[i];
    }
  }
  int t, tc;
  scanf("%d", &t);
  for (tc = 0; tc < t; tc++) {
    int a, b;
    scanf("%d%d", &a, &b);
    puts(X[a * a + b * b] != 8 ? "C" : "P");
  }
  return 0;
}
Problem p00904 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int m, n, t, c = 0;
    cin >> m >> n;
    t = m * m + n * n;
    for (int i = 0; i < 2000; i++)
      for (int j = 0; j < 2000; j++)
        if (i * i + j * j < 1)
          continue;
        else if ((m * i + n * j) % (i * i + j * j) == 0 &&
                 (m * j - n * i) % (i * i + j * j) == 0)
          c++;
    if (c <= 4)
      cout << 'P' << endl;
    else
      cout << 'C' << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int m, n, t, c = 0;
    cin >> m >> n;
    t = m * m + n * n;
    for (int i = 0; i < 10000; i++)
      for (int j = 0; j < 10000; j++)
        if (i * i + j * j < 1)
          continue;
        else if ((m * i + n * j) % (i * i + j * j) == 0 &&
                 (m * j - n * i) % (i * i + j * j) == 0)
          c++;
    if (c <= 4)
      cout << 'P' << endl;
    else
      cout << 'C' << endl;
  }
}
Problem p00906 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
typedef vector<vector<int>> Mat;
const int inf = 1e9;
const int mod = 1e9 + 7;

int N, M, A, B, C, T;

Mat mulMatMat(const Mat &a, const Mat &b) {
  int n = a.size();
  int m = b[0].size();
  assert(a[0].size() == b.size());
  Mat c(n, V(m, 0));
  rep(i, n) {
    rep(j, m) {
      rep(k, a[0].size()) { (c[i][j] += a[i][k] * b[k][j]) %= M; }
    }
  }
  return c;
}

V mulMatVec(const Mat &a, const V &v) {
  int n = a.size();
  assert(a[0].size() == v.size());
  V c(n, 0);
  rep(i, n) {
    rep(j, a[0].size()) { (c[i] += a[i][j] * v[j]) %= M; }
  }
  return c;
}

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

  while (cin >> N >> M >> A >> B >> C >> T, N) {
    V s(N + 2);
    rep2(i, 1, N + 1) cin >> s[i];

    VV a(N + 2, V(N + 2));
    rep2(i, 1, N + 1) { a[i][i - 1] = A, a[i][i] = B, a[i][i + 1] = C; }

    for (; T > 0; T >>= 1) {
      if (T & 1) {
        s = mulMatVec(a, s);
      }
      a = mulMatMat(a, a);
    }

    rep2(i, 1, N + 1) { cout << (i != 1 ? " " : "") << s[i]; }
    cout << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

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

  int N, M, A, B, C, T;
  int num_case = 0;
  while (cin >> N >> M >> A >> B >> C >> T, N) {
    V s1(N + 2);
    rep2(i, 1, N + 1) cin >> s1[i];
    VV v;
    map<V, int> mp;
    int t = 0;
    while (true) {
      if (mp.count(s1)) {
        int offset = mp[s1];
        int L = t - offset;
        int id = (T - offset) % L + offset;
        assert(id < v.size());
        rep2(i, 1, N + 1) { cout << (i != 1 ? " " : "") << v[id][i]; }
        cout << endl;
        break;
      }
      v.emplace_back(s1);
      mp[s1] = t++;

      V s2(N + 2);
      rep2(i, 1, N + 1) {
        s2[i] = (A * s1[i - 1] + B * s1[i] + C * s1[i + 1]) % M;
      }
      s1 = s2;
    }
  }
}
Problem p00906 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll n, m, a, b, c, T;

mat mul(mat A, mat B) {
  mat res;
  res.resize(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i].push_back(0);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) {
        res[i][j] += A[i][k] * B[k][j];
        res[i][j] %= m;
      }
  return res;
}

mat mod_pow(mat x, ll y) {
  mat res;
  res.resize(n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i].push_back(i == j);

  while (y) {
    if (y % 2)
      res = mul(res, x);
    x = mul(x, x);
    y /= 2;
  }
  return res;
}
int main() {
  while (1) {
    cin >> n >> m >> a >> b >> c >> T;
    if (!n && !m && !a && !b && !c && !T)
      break;

    mat s;
    s.resize(n);
    for (int i = 0; i < n; i++)
      s[i].resize(n);
    for (int i = 0; i < n; i++)
      cin >> s[i][0];

    mat x;
    x.resize(n);
    for (int i = 0; i < n; i++)
      x[i].resize(n);

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i == j)
          x[i][j] = b;
        else if (i + 1 == j && i + 1 < n)
          x[i][j] = c;
        else if (i == j + 1 && j + 1 < n)
          x[i][j] = a;
        else
          x[i][j] = 0;

    x = mod_pow(x, T);
    mat ans = mul(x, s);
    for (int i = 0; i < n - 1; i++)
      cout << ans[i][0] << " ";
    cout << ans[n - 1][0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m, a, b, c, T;
int s[2][1010];

int cal(int f, int i) {
  ll A = a * s[f][max(i - 1, 0)];
  ll B = b * s[f][i];
  ll C = c * s[f][i + 1];
  if (i == 0)
    A = 0;
  if (i == n)
    C = 0;
  return s[(f + 1) % 2][i] = (A + B + C) % m;
}

void out(int i) {
  cout << "i=" << i << " : ";
  for (int j = 0; j < n - 1; j++)
    cout << s[(i + 1) % 2][j] << " ";
  cout << s[0][n - 1] << endl;
}

int main() {
  while (1) {
    cin >> n >> m >> a >> b >> c >> T;
    if (!n && !m && !a && !b && !c && !T)
      break;

    for (int i = 0; i < n; i++)
      cin >> s[T % 2][i];
    map<vector<int>, int> M;

    for (int i = T; i > 0; i--) {
      vector<int> v;
      for (int j = 0; j < n; j++)
        v.push_back(cal(i % 2, j));
      if (M.count(v)) {
        for (int j = 0; j < n; j++)
          s[i % 2][j] = s[(i + 1) % 2][j];
        i = i % (M[v] - i);
        M.clear();
      } else if (M.size() < m * 2)
        M[v] = i;
    }

    for (int i = 0; i < n - 1; i++)
      cout << s[0][i] << " ";
    cout << s[0][n - 1] << endl;
  }
  return 0;
}
Problem p00906 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1e9;

typedef long long int lli;
typedef vector<vector<lli>> Matrix;
bool mul(Matrix &a, Matrix &b, Matrix &c) { // A*B=C
  if (a[0].size() != b.size())
    return false;
  int n = a.size();
  int m = b.size();
  int l = b[0].size();
  Matrix ret(n, vector<lli>(l, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        ret[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  c = ret;
  return true;
}

int main() {
  while (1) {
    int n, m, a, b, c, t;
    cin >> n >> m >> a >> b >> c >> t;
    if (n == 0)
      break;

    Matrix ini(n, vector<lli>(1));
    for (int i = 0; i < n; i++) {
      cin >> ini[i][0];
    }

    Matrix r(n, vector<lli>(n));
    Matrix mat(n, vector<lli>(n, 0));
    for (int i = 0; i < n; i++) {
      r[i][i] = b;
      mat[i][i] = 1;
      if (i != 0)
        r[i][i - 1] = a;
      if (i != n - 1)
        r[i][i + 1] = c;
    }
    for (int i = log2(t); i >= 0; i--) {
      mul(mat, mat, mat);
      if ((t & 1 << i) != 0) {
        mul(mat, r, mat);
      }
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          mat[j][k] = mat[j][k] % m;
        }
      }
    }

    mul(mat, ini, ini);
    for (int i = 0; i < n - 1; i++) {
      cout << ini[i][0] % m << " ";
    }
    cout << ini[n - 1][0] % m << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;

int main() {
  while (1) {
    int n, m, a, b, c, t;
    cin >> n >> m >> a >> b >> c >> t;
    if (n == 0)
      break;

    vector<int> s(n + 2, 0);
    for (int i = 1; i <= n; i++) {
      cin >> s[i];
    }
    vector<vector<int>> smem;
    smem.push_back(s);

    map<vector<int>, int> seq;
    seq[s] = 0;
    for (int i = 1; i <= t; i++) {
      vector<int> tmp(n + 2, 0);
      for (int j = 1; j <= n; j++) {
        tmp[j] = (a * s[j - 1] + b * s[j] + c * s[j + 1]) % m;
      }
      for (int j = 1; j <= n; j++) {
        s[j] = tmp[j];
      }
      smem.push_back(s);
      if (seq.count(tmp) == 0) {
        seq[tmp] = i;
      } else {
        int x = seq[tmp] + (t - i) % (i - seq[tmp]);
        s = smem[x];
        break;
      }
    }
    for (int i = 1; i < n; i++) {
      cout << s[i] << " ";
    }
    cout << s[n] << endl;
  }
  return 0;
}
Problem p00906 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, A, B, C, T;

mat calc(mat &X, mat &Y) {
  mat ret(X.size(), vec(Y[0].size()));

  int row = X.size();
  int colume = Y[0].size();
  if (X[0].size() != Y.size())
    return ret;
  for (int i = 0; i < row; i++) {
    for (int j = 0; j < colume; j++) {
      for (int k = 0; k < X[i].size(); k++) {
        ret[i][j] += (X[i][k] * Y[k][j]) % M;
        ret[i][j] %= M;
      }
    }
  }

  return ret;
}

mat dfs(mat &X, int Times) {
  if (Times == 0) {
    mat ret(X.size(), vec(X[0].size()));
    for (int i = 0; i < X.size() && i < X[0].size(); i++)
      ret[i][i] = 1;
    return ret;
  }
  auto Y = dfs(X, Times / 2);
  auto YY = calc(Y, Y);
  if (Times % 2) {
    auto ret = calc(X, YY);
    return ret;
  } else {
    return YY;
  }
}

void solve() {
  mat d(N, vec(N));
  mat v(N, vec(1));
  for (auto &i : v)
    for (auto &j : i)
      cin >> j;
  for (int i = 0; i < N; i++) {
    d[i][i] = B;
    if (i + 1 < N)
      d[i + 1][i] = A;
    if (i + 1 < N)
      d[i][i + 1] = C;
  }

  auto d_start = dfs(d, T);
  auto ans = calc(d_start, v);

  for (int i = 0; i < N - 1; i++)
    cout << ans[i][0] << " ";
  cout << ans[N - 1][0] << endl;
}

int main() {
  while (cin >> N >> M >> A >> B >> C >> T, A || B || C || N || M || T)
    solve();

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, A, B, C, T;

void solve() {
  mat s(1, vec(N));
  map<vec, int> m;
  for (auto &i : s[0])
    cin >> i;

  ll start = 0;
  ll end = T;
  for (int t = 1; t <= T; t++) {
    s.push_back(vec(N));
    if (m.find(s[t - 1]) != m.end()) {
      start = m[s[t - 1]];
      end = t - 2;
      break;
    }
    m[s[t - 1]] = t - 1;
    for (int i = 0; i < N; i++) {
      ll a = (A * (i - 1 < 0 ? 0 : s[t - 1][i - 1])) % M;
      ll b = (B * s[t - 1][i]) % M;
      ll c = (C * (i + 1 < N ? s[t - 1][i + 1] : 0)) % M;
      s[t][i] = (a + b + c) % M;
    }
  }

  if (end != T) {
    T = (T - start) % (end - start + 1) + start;
  }

  for (auto i : s[T])
    cout << i << " ";
  cout << endl;
}

int main() {
  while (cin >> N >> M >> A >> B >> C >> T, A || B || C || N || M || T)
    solve();

  return 0;
}
Problem p00907 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int D;
double V[100];

double absd(double x) {

  if (x < 0) {
    return -x;
  }

  return x;
}

double interpolate(int n, int E) {

  double sum = 0.0;

  for (int k = 0; k < D + 3; k++) {

    if (k == n || k == E) {
      continue;
    }

    double p = V[k];

    for (int i = 0; i < D + 3; i++) {

      if (i != k && i != n && i != E) {
        p *= (n - i) / (double)(k - i);
      }
    }

    sum += p;
  }

  return sum;
}

bool outlier(int E) {

  for (int i = 0; i < D + 3; i++) {

    if (i == E) {
      continue;
    }

    double p = interpolate(i, E);

    if (absd(p - V[i]) > 0.1) {
      return false;
    }
  }

  return true;
}

int main() {

  while (cin >> D && D != 0) {

    for (int i = 0; i < D + 3; i++) {
      cin >> V[i];
    }

    for (int i = 0; i < D + 3; i++) {

      if (outlier(i)) {

        cout << i << endl;
        break;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int D;
double V[5];

double absd(double x) {

  if (x < 0) {
    return -x;
  }

  return x;
}

double interpolate(int n, int E) {

  double sum = 0.0;

  for (int k = 0; k < D + 3; k++) {

    if (k == n || k == E) {
      continue;
    }

    double p = V[k];

    for (int i = 0; i < D + 3; i++) {

      if (i != k && i != n && i != E) {
        p *= (n - i) / (double)(k - i);
      }
    }

    sum += p;
  }

  return sum;
}

bool outlier(int E) {

  for (int i = 0; i < D + 3; i++) {

    if (i == E) {
      continue;
    }

    double p = interpolate(i, E);

    if (absd(p - V[i]) > 0.1) {
      return false;
    }
  }

  return true;
}

int main() {

  while (cin >> D && D != 0) {

    for (int i = 0; i < D + 3; i++) {
      cin >> V[i];
    }

    for (int i = 0; i < D + 3; i++) {

      if (outlier(i)) {

        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long lli;
typedef pair<int, lli> P;
#define MAX 100001

P par[MAX];
lli rank[MAX];

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

P find(int x) {
  if (par[x].first == x)
    return par[x];
  else {
    lli tmp = par[x].second;
    par[x] = find(par[x].first);
    par[x].second += tmp;
    return par[x];
  }
}

void unite(int x, int y, lli z) {
  P X = find(x), Y = find(y);
  x = X.first;
  y = Y.first;
  if (x == y)
    return;
  if (rank[x] < rank[y]) {
    par[x].first = y;
    par[x].second = (z - X.second + Y.second);
  } else {
    par[y].first = x;
    par[y].second = -(z - X.second + Y.second);
    if (rank[x] == rank[y])
      rank[x]++;
  }
}

int N, M;

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    init();
    for (int i = 0; i < M; i++) {
      char ch;
      lli a, b, c;
      cin >> ch >> a >> b;
      if (ch == '?') {
        P A = find(a);
        P B = find(b);
        if (A.first != B.first)
          cout << "UNKNOWN" << endl;
        else {
          cout << A.second - B.second << endl;
        }
      } else {
        cin >> c;
        unite(a, b, c);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, m, x, sum, flg;
string str;
int t[100001], u[100001];
int dfs(int x) {
  if (u[x] == 1)
    return x;
  return dfs(t[x]);
}
int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < 100001; i++)
      u[i] = 0;
    t[1] = u[1] = 1;
    sum = 0;
    for (int i = 0; i < n - 1; i++)
      cin >> t[i + 2];
    for (int i = 0; i < m; i++) {
      cin >> str >> x;
      if (str == "Q") {
        sum += dfs(t[x]);
      } else {
        u[x] = 1;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
struct Info {
  int par, weight;
};
const int MAX = 100005;
const int INF = (1 << 28);

class UnionFind {
public:
  Info info[MAX];

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

  Info find(int x) {
    if (info[x].par == x)
      return info[x];

    Info tmp = find(info[x].par);
    info[x].weight += tmp.weight;
    info[x].par = tmp.par;
    return info[x];
  }

  void unite(int x, int y, int w) {
    Info X = find(x);
    Info Y = find(y);
    // W[x]+w = W[y]

    info[Y.par].par = X.par;
    info[Y.par].weight += w + info[x].weight - info[y].weight;
  }

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

  int getValue(int x) { return info[x].weight; }
};

int N, M;
bool used[MAX];
UnionFind uf;

void init() {
  memset(used, false, sizeof(used));
  uf.init(N + 1);
}

void update() {

  int a, b, c;
  cin >> a >> b >> c;
  uf.unite(a, b, c);
}

void print() {
  int a, b;
  cin >> a >> b;
  if (uf.same(a, b))
    cout << uf.getValue(b) - uf.getValue(a) << endl;
  else
    cout << "UNKNOWN" << endl;
}

int main() {

  while (cin >> N >> M && N + M) {
    init();
    for (int i = 0; i < M; i++) {
      char c;
      cin >> c;
      if (c == '!')
        update();
      else
        print();
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 100005;

class UnionFind {
public:
  int par[MAX];
  int rank[MAX];

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

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y])
      par[x] = par[y];
    else {
      par[y] = par[x];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

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

int N, M, val[MAX];
bool used[MAX];
UnionFind uf;
vector<int> V;

void init() {
  memset(used, false, sizeof(used));
  memset(val, 0, sizeof(val));
  uf.init(N + 1);
  V.clear();
}

void update() {

  int a, b, c;
  cin >> a >> b >> c;
  if (!uf.same(a, b)) {
    if (!used[a]) {
      V.push_back(a);
      used[a] = true;
    }
    if (!used[b]) {
      V.push_back(b);
      used[b] = true;
    }

    int diff = val[a] - val[b] + c;
    for (int i = 0; i < V.size(); i++) {
      if (uf.same(V[i], b))
        val[V[i]] += diff;
    }
    uf.unite(a, b);
  }
}

void print() {
  int a, b;
  cin >> a >> b;
  if (uf.same(a, b))
    cout << val[b] - val[a] << endl;
  else
    cout << "UNKNOWN" << endl;
}

int main() {

  while (cin >> N >> M && N + M) {
    init();
    for (int i = 0; i < M; i++) {
      char c;
      cin >> c;
      if (c == '!')
        update();
      else
        print();
    }
  }
  return 0;
}