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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  long n;
  cin >> n;
  long nod = n * (n + 1) * (n + 2) / 6;
  long edg = 0;
  rep(i, n - 1) {
    long u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    edg += u * (n - v + 1);
  }
  cout << nod - edg << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  int from;
  int to;
  long cost;
};

vector<int> par;

void Initpar(int n) {
  par = vector<int>(n);
  rep(i, n) par.at(i) = i;
}

int root(int x) {
  // cout << "inroot" << x << " " << par.at(x) << endl;
  // cout << (x==par.at(x)) << endl;
  if (x == par.at(x)) {
    // cout << "why" << x << endl;
    return x;
  } else
    return par.at(x) = root(par.at(x));
}

void unite(int x, int y) {
  // cout << "st" << x << y << endl;
  // rep(i,3) cout << par.at(i);
  // cout << endl;
  int rx = root(x);
  // cout << "ha" << rx << endl;
  int ry = root(y);
  // cout << "mid" << endl;
  if (rx == ry)
    return;
  if (rx < ry)
    par.at(ry) = rx;
  else
    par.at(rx) = ry;
}

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

void update_par() {
  for (int i = 0; i < par.size(); i++) {
    root(i);
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> Graph(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    Graph.at(u).push_back({u, v, 1});
    Graph.at(v).push_back({v, u, 1});
  }
  long ans = 0;
  rep(i, n) {

    long tmp = 0;
    Initpar(n);
    REP(j, i, n) {
      bool noentry = true;
      for (Edge ed : Graph.at(j)) {
        int nv = ed.to;
        // cout << j << 'u' << endl;
        // cout << j+1 << " " << nv+1 << "#" << endl;
        if (i <= nv && nv < j) {
          // cout << i+1 << " " << j+1 << endl;
          if (noentry) {
            noentry = false;
            // cout << "start" << endl;
            par.at(j) = root(nv);
            // cout << "end" << endl;
          } else {
            tmp--;
            // cout << "start1" << endl;
            unite(j, nv);
            // cout << "end2" << endl;
          }
        }
      }
      if (noentry) {
        par.at(j) = j;
        tmp++;
      }
      // cout << "i" << i << endl;
      // cout << i+1 << " " << j+1 << " " << tmp << endl;
      ans += tmp;
    }
  }
  cout << ans << endl;
}