Problem p00909 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 100002

int nl;
Pii pt[SUPnl]; // pt[n].F == n's parent, pt[n].S == w[pt[n].F]-w[n]

void init() {
  REP(ni, nl) { pt[ni] = MP(ni, 0); }
}

Pii getRoot(int n) {
  if (pt[n].F == n)
    return pt[n];
  Pii rpnt = getRoot(pt[n].F);
  pt[n] = MP(rpnt.F, rpnt.S + pt[n].S);
  return pt[n];
}

void printW(int a, int b) {
  Pii rat = getRoot(a);
  Pii rbt = getRoot(b);
  if (rat.F != rbt.F) // cout<<"UNKNOWN"<<endl;
    printf("UNKNOWN\\n");
  else // cout<< rbt.S - rat.S << endl;
    printf("%d\\n", rbt.S - rat.S);
}

void setW(int a, int b, int w) {
  Pii rat = getRoot(a);
  Pii rbt = getRoot(b);
  if (rat.F == rbt.F)
    return;
  if (rat.S + w >= rbt.S) {
    pt[rbt.F] = MP(rat.F, rat.S + w - rbt.S);
  } else {
    pt[rat.F] = MP(rbt.F, rbt.S - w - rat.S);
  }
}

int main() {
  while (true) {
    int ol;
    cin >> nl >> ol; // scanf("%d%d", &nl, &ol);
    if (!nl)
      return 0;
    init();
    REP(oi, ol) {
      char ot;
      int a, b, w;
      cin >> ot; // scanf(" %c ", &ot);
      if (ot == '!') {
        cin >> a >> b >> w; // scanf("%d%d%d",&a,&b,&w);
        setW(a - 1, b - 1, w);
      }
      if (ot == '?') {
        cin >> a >> b; // scanf("%d%d",&a,&b);
        printW(a - 1, b - 1);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define DEBUG(x) cout << #x << ": " << x << endl

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INT(a) int((a) + 1e-9)
int DX[4] = {1, 0, -1, 0};
int DY[4] = {0, 1, 0, -1};

typedef pair<int, int> Pii;

#define SUPnl 100002

int nl;
Pii p[SUPnl]; // p[n].F == n's parent, p[n].S == w[p[n].F]-w[n]

void init() {
  REP(ni, nl) { p[ni] = MP(-1, 0); }
}

Pii getRoot(int n) {
  int rn = n;
  int w = 0;
  while (p[rn].F != -1) {
    w += p[rn].S;
    rn = p[rn].F;
  }
  if (rn != n)
    p[n] = MP(rn, w);
  return MP(rn, w);
}

void printW(int a, int b) {
  Pii rat = getRoot(a);
  Pii rbt = getRoot(b);
  if (rat.F != rbt.F)
    cout << "UNKNOWN" << endl;
  else
    cout << rbt.S - rat.S << endl;
}

void setW(int a, int b, int w) {
  Pii rat = getRoot(a);
  Pii rbt = getRoot(b);
  if (rat.F == rbt.F)
    return;
  if (rat.S + w >= rbt.S) {
    p[rbt.F] = MP(rat.F, rat.S + w - rbt.S);
  } else {
    p[rat.F] = MP(rbt.F, rbt.S - w - rat.S);
  }
}

int main() {
  while (true) {
    int ol;
    cin >> nl >> ol;
    if (!nl)
      return 0;
    init();
    REP(oi, ol) {
      char ot;
      int a, b, w;
      cin >> ot;
      if (ot == '!') {
        cin >> a >> b >> w;
        setW(a - 1, b - 1, w);
      }
      if (ot == '?') {
        cin >> a >> b;
        printW(a - 1, b - 1);
      }
    }
  }
}