Problem p00326 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<vector<int>> f, e;

struct node {
  int id, ty;
  node() {}
  void init(int x, int y) {
    id = x;
    ty = y;
  }
  bool operator<(const node &a) const {
    for (int i = 0; i < k; i++) {
      if (f[id][e[ty][i]] != f[a.id][e[ty][i]])
        return f[id][e[ty][i]] > f[a.id][e[ty][i]];
    }
    return 0;
  }
};
node nodes[24][114514];
vector<int> G[114514];
set<node> ns[24];
signed main() {
  cin >> n >> k;
  f.resize(n, vector<int>(k));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      // cin>>f[i][j];
      scanf("%d", &f[i][j]);
    }
  }
  int d;
  cin >> d;

  int indeg[n];
  memset(indeg, 0, sizeof(indeg));
  for (int i = 0; i < d; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    indeg[b]++;
  }
  map<vector<int>, int> m;
  vector<int> v(k);
  for (int i = 0; i < k; i++)
    v[i] = i;
  int cnt = 0;
  do {
    for (int i = 0; i < n; i++) {
      nodes[cnt][i].init(i, cnt);
    }
    e.push_back(v);
    m[v] = cnt++;
  } while (next_permutation(v.begin(), v.end()));

  for (int j = 0; j < cnt; j++) {
    for (int i = 0; i < n; i++) {
      if (indeg[i] == 0)
        ns[j].insert(nodes[j][i]);
      // if(indeg[i]==0) cout<<":"<<i<<endl;
    }
  }

  for (int i = 0; i < k; i++)
    cin >> v[i], v[i]--;
  // cout<<m.count(v)<<endl;
  int ans = 0, idx = m[v];
  // cout<<idx<<endl;

  int r;
  cin >> r;
  while (r--) {
    int p;
    cin >> p;
    while (ans < p) {
      int id = (ns[idx].begin())->id;
      for (int i = 0; i < cnt; i++) {
        // ns[i].insert(nodes[i][id]);
        // cout<<id<<":"<<ns[i].count(nodes[i][id])<<endl;
        // cout<<ns[i].size()<<" "<<ns[i].begin()->id<<endl;
        // cout<<(*ns[i].begin()==nodes[i][id])<<endl;
        ns[i].erase(nodes[i][id]);
      }
      for (int u : G[id]) {
        indeg[u]--;
        if (indeg[u] == 0) {
          for (int i = 0; i < cnt; i++) {
            ns[i].insert(nodes[i][u]);
          }
        }
      }
      // cout<<id+1<<endl;
      printf("%d\\n", id + 1);
      ans++;
    }
    for (int i = 0; i < k; i++)
      cin >> v[i], v[i]--;
    idx = m[v];
    // cout<<idx<<endl;
  }
  while (ans < n) {
    int id = ns[idx].begin()->id;
    for (int i = 0; i < cnt; i++) {
      // cout<<":"<<ns[i].count(nodes[i][id])<<endl;
      ns[i].erase(nodes[i][id]);
    }
    for (int u : G[id]) {
      indeg[u]--;
      if (indeg[u] == 0) {
        for (int i = 0; i < cnt; i++) {
          ns[i].insert(nodes[i][u]);
        }
      }
    }
    // cout<<id+1<<endl;
    printf("%d\\n", id + 1);
    ans++;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
struct node {
  int id;
  vector<int> f, e;
  node() {}
  void init(int x, vector<int> fb, vector<int> eb) {
    id = x;
    f = fb;
    e = eb;
  }
  bool operator<(const node &a) const {
    for (int i = 0; i < k; i++) {
      if (f[e[i]] != a.f[e[i]])
        return f[e[i]] > a.f[e[i]];
    }
    return 0;
  }
};
vector<int> G[114514];
node nodes[24][114514];
set<node> ns[24];
signed main() {
  cin >> n >> k;
  vector<vector<int>> f(n, vector<int>(k));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      // cin>>f[i][j];
      scanf("%d", &f[i][j]);
    }
  }
  int d;
  cin >> d;

  int indeg[n];
  memset(indeg, 0, sizeof(indeg));
  for (int i = 0; i < d; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    indeg[b]++;
  }
  map<vector<int>, int> m;
  vector<int> v(k);
  for (int i = 0; i < k; i++)
    v[i] = i;
  int cnt = 0;
  do {
    for (int i = 0; i < n; i++) {
      nodes[cnt][i].init(i, f[i], v);
    }
    m[v] = cnt++;
  } while (next_permutation(v.begin(), v.end()));

  for (int j = 0; j < cnt; j++) {
    for (int i = 0; i < n; i++) {
      if (indeg[i] == 0)
        ns[j].insert(nodes[j][i]);
      // if(indeg[i]==0) cout<<":"<<i<<endl;
    }
  }

  for (int i = 0; i < k; i++)
    cin >> v[i], v[i]--;
  // cout<<m.count(v)<<endl;
  int ans = 0, idx = m[v];
  // cout<<idx<<endl;

  int r;
  cin >> r;
  while (r--) {
    int p;
    cin >> p;
    while (ans < p) {
      int id = (ns[idx].begin())->id;
      for (int i = 0; i < cnt; i++) {
        // ns[i].insert(nodes[i][id]);
        // cout<<id<<":"<<ns[i].count(nodes[i][id])<<endl;
        // cout<<ns[i].size()<<" "<<ns[i].begin()->id<<endl;
        // cout<<(*ns[i].begin()==nodes[i][id])<<endl;
        ns[i].erase(nodes[i][id]);
      }
      for (int u : G[id]) {
        indeg[u]--;
        if (indeg[u] == 0) {
          for (int i = 0; i < cnt; i++) {
            ns[i].insert(nodes[i][u]);
          }
        }
      }
      // cout<<id+1<<endl;
      printf("%d\\n", id + 1);
      ans++;
    }
    for (int i = 0; i < k; i++)
      cin >> v[i], v[i]--;
    idx = m[v];
    // cout<<idx<<endl;
  }
  while (ans < n) {
    int id = ns[idx].begin()->id;
    for (int i = 0; i < cnt; i++) {
      // cout<<":"<<ns[i].count(nodes[i][id])<<endl;
      ns[i].erase(nodes[i][id]);
    }
    for (int u : G[id]) {
      indeg[u]--;
      if (indeg[u] == 0) {
        for (int i = 0; i < cnt; i++) {
          ns[i].insert(nodes[i][u]);
        }
      }
    }
    // cout<<id+1<<endl;
    printf("%d\\n", id + 1);
    ans++;
  }
  return 0;
}