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

#include <bits/stdc++.h>
using namespace std;
#define __ << ' ' <<
#define ___ << ' '
#define bash push_back
#define ALL(x) x.begin(), x.end()
// #define int long long

struct IoSetup {
  IoSetup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
  }
} IoSetup;

using ll = long long;
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int SMOD = 1000000007;
constexpr int NMOD = 998244353;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

inline bool inside(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
template <class T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}
template <class T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
inline int qp(int a, ll b, int mo) {
  ll ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return ans;
}

int a[100], b[100], c[100], d[100];
int n, m, q;
int dfs(int i, int arr[]) {
  if (i == n) {
    int ret = 0;
    for (int j = 0; j < q; j++) {
      if (arr[b[j]] - arr[a[j]] == c[j])
        ret += d[j];
    }
    return ret;
  }
  int ret = 0;
  for (int j = i ? arr[i - 1] : 1; j <= m; j++) {
    arr[i] = j;
    chmax(ret, dfs(i + 1, arr));
  }
  return ret;
}

signed main() {

  cin >> n >> m >> q;

  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--, b[i]--;
  }

  int arr[10];

  cout << dfs(0, arr) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define __ << ' ' <<
#define ___ << ' '
#define bash push_back
#define ALL(x) x.begin(), x.end()
// #define int long long

struct IoSetup {
  IoSetup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
  }
} IoSetup;

using ll = long long;
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int SMOD = 1000000007;
constexpr int NMOD = 998244353;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

inline bool inside(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
template <class T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}
template <class T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
inline int qp(int a, ll b, int mo) {
  ll ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return ans;
}

template <typename T> struct WeightedUnionFind {
  vector<int> par, rank;
  vector<T> diff_weight;

  // SUM_UNITY アベール群の単位元
  WeightedUnionFind(int n = 1, T SUM_UNITY = 0)
      : par(n + 5), rank(n + 5), diff_weight(n + 5) {
    for (int i = 0; i < n + 5; i++)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    int r = root(par[x]);
    diff_weight[x] += diff_weight[par[x]];
    return par[x] = r;
  }

  T weight(int x) {
    root(x);
    return diff_weight[x];
  }

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

  bool merge(int x, int y, T w) {
    w += weight(x) - weight(y);
    if ((x = root(x)) == (y = root(y)))
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    rank[x] += (rank[x] == rank[y]);

    par[y] = x, diff_weight[y] = w;
    return true;
  }

  T diff(int x, int y) { return weight(y) - weight(x); }
};

signed main() {

  int n, m, q;

  cin >> n >> m >> q;

  vector<int> a(q), b(q), c(q), d(q);

  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  int mv = 0;
  vector<int> v(q);

  for (int i = 0; i < q; i++) {
    v[i] = i;
  }
  do {
    WeightedUnionFind<int> uf(n);
    uf.merge(a[v[0]], b[v[0]], c[v[0]]);
    int sum = 0;
    for (int j = 0; j < q; j++) {
      if (uf.same(a[v[j]], b[v[j]])) {
        if (uf.diff(a[v[j]], b[v[j]]) == c[v[j]]) {
          sum += d[v[j]];
        } else
          break;
      } else {
        uf.merge(a[v[j]], b[v[j]], c[v[j]]);
        sum += d[v[j]];
      }
    }
    chmax(mv, sum);
  } while (next_permutation(ALL(v)));

  cout << mv << endl;

  return 0;
}