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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, u, v;
long long ans;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ans += i * 1LL * (n - i + 1);
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    ans -= u * 1LL * (n - v + 1);
  }
  cout << ans << E;
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
ll powerm(ll base, ll exp, ll mod) {
  ll ans = 1;
  while (exp) {
    if (exp & 1)
      ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
/*//DSU
int parent[200005];
int rankr[200005];
int find_set(int v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}
void make_set(int v) {
    parent[v] = v;
    rankr[v] = 0;
}
void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (rankr[a] < rankr[b])
            swap(a, b);
        parent[b] = a;
        if (rankr[a] == rankr[b])
            rankr[a]++;
    }
} */
void dfs(ll node, vector<ll> graph[], unordered_map<ll, ll> &visited, ll l,
         ll r) {
  visited[node] = 1;
  for (ll i = 0; i < graph[node].size(); i++) {
    ll ele = graph[node][i];
    if (visited[ele] == 0 && ele >= l && ele <= r) {
      dfs(ele, graph, visited, l, r);
    }
  }
  return;
}
int main() {
  fast;
  ll n;
  cin >> n;
  vector<ll> graph[n + 1];
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i; j <= n; j++) {
      unordered_map<ll, ll> visited;
      for (ll k = i; k <= j; k++) {
        if (visited[k] == 0) {
          dfs(k, graph, visited, i, j);
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}