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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int status_array[4];
};

Info info[50000];
int N, K;
vector<int> V[50000];
bool outputed[50000];
int in_num[50000];
int order_table[33][4] = {
    {0, 0, 0, 0}, {0, 1, 0, 0}, {1, 0, 0, 0}, {0, 1, 2, 0}, {0, 2, 1, 0},
    {1, 0, 2, 0}, {1, 2, 0, 0}, {2, 0, 1, 0}, {2, 1, 0, 0}, {0, 1, 2, 3},
    {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1},
    {1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2},
    {1, 3, 2, 0}, {2, 0, 1, 3}, {2, 0, 3, 1}, {2, 1, 0, 3}, {2, 1, 3, 0},
    {2, 3, 0, 1}, {2, 3, 1, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {3, 1, 0, 2},
    {3, 1, 2, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}};

bool value_func(int task_a, int task_b, int order) {
  for (int i = 0; i < K; i++) {
    if (info[task_a].status_array[order_table[order][i]] !=
        info[task_b].status_array[order_table[order][i]]) {
      return info[task_a].status_array[order_table[order][i]] <
             info[task_b].status_array[order_table[order][i]];
    }
  }
  return false; // must not reach here
}

struct Data {
  Data(int arg_id, int arg_order) {
    id = arg_id;
    order = arg_order;
  }
  bool operator<(const struct Data &arg) const {
    return value_func(id, arg.id, order);
  }
  int id, order;
};

priority_queue<Data> Q[33];
vector<int> TO_PUSH[5];

int main() {

  TO_PUSH[1].push_back(0);
  TO_PUSH[2].push_back(1);
  TO_PUSH[2].push_back(2);
  for (int i = 3; i <= 8; i++)
    TO_PUSH[3].push_back(i);
  for (int i = 9; i <= 32; i++)
    TO_PUSH[4].push_back(i);

  scanf("%d %d", &N, &K);
  for (int a = 0; a < N; a++) {
    for (int b = 0; b < K; b++) {
      scanf("%d", &info[a].status_array[b]);
    }
  }

  for (int i = 0; i < N; i++) {
    in_num[i] = 0;
    outputed[i] = false;
  }

  int D;
  scanf("%d", &D);

  int from, to;
  for (int loop = 0; loop < D; loop++) {
    scanf("%d %d", &from, &to);
    from--;
    to--;
    V[from].push_back(to);
    in_num[to]++;
  }

  int value_array[4];
  for (int i = 0; i < 4; i++) {
    value_array[i] = 0;
  }

  for (int i = 0; i < K; i++) {
    scanf("%d", &value_array[i]);
    value_array[i]--;
  }

  queue<int> ORDER;
  bool FLG;
  for (int i = 0; i < 33; i++) {
    FLG = true;
    for (int k = 0; k < 4; k++) {
      if (value_array[k] != order_table[i][k]) {
        FLG = false;
        break;
      }
    }
    if (FLG) {
      ORDER.push(i);
      break;
    }
  }

  int R;
  scanf("%d", &R);

  bool is_change[N];
  for (int i = 0; i < N; i++)
    is_change[i] = false;

  int m;
  for (int i = 0; i < R; i++) {
    scanf("%d", &m);
    is_change[m] = true;
    for (int a = 0; a < K; a++) {
      scanf("%d", &value_array[a]);
      value_array[a]--;
    }
    for (int k = 0; k < 33; k++) {
      FLG = true;
      for (int p = 0; p < 4; p++) {
        if (value_array[p] != order_table[k][p]) {
          FLG = false;
          break;
        }
      }
      if (FLG) {
        ORDER.push(k);
        break;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (in_num[i] == 0) {
      for (int a = 0; a < TO_PUSH[K].size(); a++) {
        Q[TO_PUSH[K][a]].push(Data(i, TO_PUSH[K][a]));
      }
    }
  }

  int dealt_num = 0, tmp_id;

  while (dealt_num < N) {

    if (outputed[Q[ORDER.front()].top().id]) {
      Q[ORDER.front()].pop();
      continue;
    }

    printf("%d\\n", Q[ORDER.front()].top().id + 1);
    outputed[Q[ORDER.front()].top().id] = true;
    dealt_num++;

    if (dealt_num == N) {
      return 0;
    }
    tmp_id = Q[ORDER.front()].top().id;
    Q[ORDER.front()].pop();
    if (is_change[dealt_num]) {
      ORDER.pop();
    }

    for (int k = 0; k < V[tmp_id].size(); k++) {
      in_num[V[tmp_id][k]]--;
      if (in_num[V[tmp_id][k]] == 0) {
        for (int a = 0; a < TO_PUSH[K].size(); a++) {
          Q[TO_PUSH[K][a]].push(Data(V[tmp_id][k], TO_PUSH[K][a]));
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int status_array[4];
};

Info info[50000];
int N, K;
vector<int> V[50000];
int in_num[50000], value_array[4], value_table[50000][4], value_index = 0;

bool value_func(int task_a, int task_b) {
  for (int i = 0; i < K; i++) {
    if (info[task_a].status_array[value_array[i]] !=
        info[task_b].status_array[value_array[i]]) {
      return info[task_a].status_array[value_array[i]] <
             info[task_b].status_array[value_array[i]];
    }
  }
  return false; // must not reach here
}

struct Data {
  Data(int arg_id) { id = arg_id; }
  bool operator<(const struct Data &arg) const {
    return value_func(id, arg.id);
  }
  int id;
};

void update_value() {
  for (int i = 0; i < K; i++) {
    value_array[i] = value_table[value_index][i];
  }
  value_index++;
}

int main() {

  scanf("%d %d", &N, &K);
  for (int a = 0; a < N; a++) {
    for (int b = 0; b < K; b++) {
      scanf("%d", &info[a].status_array[b]);
    }
  }

  for (int i = 0; i < N; i++)
    in_num[i] = 0;

  int D;
  scanf("%d", &D);

  int from, to;
  for (int loop = 0; loop < D; loop++) {
    scanf("%d %d", &from, &to);
    from--;
    to--;
    V[from].push_back(to);
    in_num[to]++;
  }

  for (int i = 0; i < K; i++) {
    scanf("%d", &value_array[i]);
    value_array[i]--;
  }

  int R;
  scanf("%d", &R);

  bool is_change[N];
  for (int i = 0; i < N; i++)
    is_change[i] = false;

  int m;
  for (int i = 0; i < R; i++) {
    scanf("%d", &m);
    is_change[m] = true;
    for (int a = 0; a < K; a++) {
      scanf("%d", &value_table[i][a]);
      value_table[i][a]--;
    }
  }

  priority_queue<Data> Q[2];
  int current = 0;

  for (int i = 0; i < N; i++) {
    if (in_num[i] == 0) {
      Q[current].push(Data(i));
    }
  }

  int dealt_num = 0, tmp_id;

  while (dealt_num < N) {
    printf("%d\\n", Q[current].top().id + 1);
    dealt_num++;

    if (dealt_num == N) {
      return 0;
    }
    tmp_id = Q[current].top().id;
    Q[current].pop();
    if (is_change[dealt_num]) {
      update_value();
      while (!Q[current].empty()) {
        Q[1 - current].push(Q[current].top());
        Q[current].pop();
      }
      current = 1 - current;
    }

    for (int k = 0; k < V[tmp_id].size(); k++) {
      in_num[V[tmp_id][k]]--;
      if (in_num[V[tmp_id][k]] == 0) {
        Q[current].push(Data(V[tmp_id][k]));
      }
    }
  }

  return 0;
}