Problem:
スケジューラ
あなたはユニークなオペレーティングシステム「ウンズグネ１５」の開発に取り組んでおり、性能を決定付けるスケジューラの設計に頭を悩ませている。スケジューラとは、実行すべき処理をタスクという単位で表現し、それらをどの順序で実行するかを決定するプログラムである。スケジューラはタスクに１から
N
の番号をつけて管理する。全てのタスクは
K
個の属性
f
1
,
f
2
,...,
f
K
を持ち、各属性にはそれぞれ固有の値が設定されている。ただし、ある２つのタスクについて、対応する属性の値すべてが同じになることはない。
あるタスクには、そのタスクの実行を始める前までに実行を完了していなければならないタスクが与えられることがある。タスクＡがタスクＢの前に完了していなければならないことを「タスクＡ → タスクＢ」と表す。例えば、タスク１ → タスク２、タスク３ → タスク２という関係があれば、タスク２を処理する前にタスク１とタスク３の両方の処理が終わっていなければならない。このような関係をタスク間の依存関係という。ただし、あるタスクから依存関係をたどっていって、そのタスクにたどり着くことはない。
スケジューラは依存関係に従って、実行順序を決定する。しかし、依存関係だけでは順序が一通りに定まらない場合がある。そのような場合は、各タスクが持つ属性の値によって、次に処理するタスクを選択する。
ウンズグネ１５のタスクは属性を複数もつため、すべての属性の値を考慮して実行順序を決定する必要がある。そのために、属性を比較する順番を定める評価順序を用いる。評価順序が最も先の属性を比較し、その属性の値が最も大きいタスクを選択する。そのようなタスクが複数ある場合は、評価順序がその次の属性で比較し、以下同様な手順を繰り返す。例えば、以下の3 つの属性を持つ3 つのタスクについて考える。
タスク＼属性
f
1
f
2
f
3
X
3
3
2
Y
3
2
2
Z
3
1
3
評価順序が
f
1
f
2
f
3
、
f
2
f
1
f
3
、または
f
2
f
3
f
1
に設定されている場合は、タスクX が選ばれる。また、評価順序が
f
1
f
3
f
2
、
f
3
f
1
f
2
、または
f
3
f
2
f
1
に設定されている場合はタスクZ が選ばれる。
ウンズグネ１５のスケジューラの特徴は、属性の評価順序が途中で何度でも変更できることである。評価順序は、ある個数のタスクの実行が完了した時点で変更できる。ただし、スケジューラが最初に使う評価順序はあらかじめ決まっている。
各タスクの属性の値、タスクの依存関係、評価順序の変更情報が与えられたとき、タスクを実行する順序を報告するプログラムを作成せよ。
Input
入力は以下の形式で与えられる。
N
K
f
1,1
f
1,2
...
f
1,K
f
2,1
f
2,2
...
f
2,K
:
f
N,1
f
N,2
...
f
N,K
D
a
1
b
1
a
2
b
2
:
a
D
b
D
e
0,1
e
0,2
...
e
0,K
R
m
1
e
1,1
e
1,2
...…
e
1,K
m
2
e
2,1
e
2,2
...…
e
2,K
:
m
R
e
R,1
e
R,2
...…
e
R,K
１行目に、タスクの数
N
(2 ≤
N
≤ 50000) と、各タスクが持つ属性の数
K
(1 ≤
K
≤ 4) が与えられる。続く
N
行に、タスク
i
が持つ属性の値
f
i,j
(1 ≤
f
i,j
≤ 100000) が与えられる。続く１行に、依存関係の個数
D
(0 ≤
D
≤ 200000) が与えられる。続く
D
行に依存関係
a
i
→
b
i
(1 ≤
a
i
,
b
i
≤
N
) が与えられる。
続く１行に、最初の評価順序
e
0,j
(1 ≤
e
0,j
≤
K
) が与えられる。続く１行に、評価順序の変更回数
R
(0 ≤
R
<
N
) が与えられる。続く
R
行に、評価順序の変更情報が与えられる。
i
回目の変更情報は、実行が完了したタスクの個数
m
i
(1 ≤
m
i
<
N
) と評価順序
e
i,j
(1 ≤
e
i,j
≤
K
) からなり、全部で
m
i
個のタスクの実行が完了した時点で、評価順序を
e
i,1
,
e
i,2
,... ,
e
i,K
に変更することを示す。
評価順序の変更情報は以下の条件を満たす。
e
i,1
,
e
i,2
,...,
e
i,K
中に同じ値は２つ以上現れない。
i
<
j
のとき、
m
i
<
m
j
である。
Output
スケジューラが処理する順番に、タスクの番号を出力する。
Sample Input 1
5 3
1 5 2
3 8 5
1 2 3
5 5 5
4 8 2
0
1 2 3
2
2 2 3 1
4 3 1 2
Sample Output 1
4
5
2
1
3
Sample Input 2
5 2
1 1
2 1
3 1
4 4
5 2
3
1 4
2 4
2 5
1 2
1
3 2 1
Sample Output 2
3
2
5
1
4

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int N, K;
	cin >> N >> K;
	int fac = 1;
	rep(i, 1, K + 1) fac *= i;
	
	vector F(N, vector<int>(K));
	rep(i, 0, N) rep(j, 0, K) cin >> F[i][j];
	
	int D;
	cin >> D;
	vector<vector<int>> G(N);
	rep(i, 0, D) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		G[a].push_back(b);
	}
	
	vector<int> perm(K);
	rep(i, 0, K) perm[i] = i;
	
	vector rank(fac, vector<int>(N));
	map<int, int> ID;
	int cnter = 0;
	do {
		vector<int> S(N);
		rep(i, 0, N) S[i] = i;
		
		sort(S.begin(), S.end(), [&](int i, int j) {
			rep(k, 0, K) {
				if (F[i][perm[k]] != F[j][perm[k]]) return F[i][perm[k]] < F[j][perm[k]];
			}
			return true;
		});
		
		reverse(S.begin(), S.end());
		
		rep(i, 0, N) rank[cnter][S[i]] = i;
		
		int hash = 0;
		rep(i, 0, K) {
			hash *= 10;
			hash += perm[i] + 1;
		}
		ID[hash] = cnter;
		
		++cnter;
	} while (next_permutation(perm.begin(), perm.end()));
	
	int hash = 0;
	rep(i, 0, K) {
		int E;
		cin >> E;
		hash *= 10;
		hash += E;
	}
	int nowFunc = ID[hash];
	
	int R;
	cin >> R;
	vector<int> func(N, -1);
	rep(i, 0, R) {
		int hash = 0;
		int m;
		cin >> m;
		rep(j, 0, K) {
			int E;
			cin >> E;
			hash *= 10;
			hash += E;
		}
		func[m - 1] = ID[hash];
	}
	
	vector<int> deg(N);
	rep(v, 0, N) {
		for (int to : G[v]) ++deg[to];
	}
	
	vector<set<pair<int, int>>> que(fac);
	rep(v, 0, N) if (deg[v] == 0) {
		rep(i, 0, fac) que[i].insert({rank[i][v], v});
	}
	
	rep(itr, 0, N) {
		auto p = *que[nowFunc].begin();
		int v = p.second;
		
		cout << v + 1 << '\n';
		
		rep(i, 0, fac) que[i].erase({rank[i][v], v});
		
		for (int to : G[v]) if (--deg[to] == 0) {
			rep(i, 0, fac) que[i].insert({rank[i][to], to});
		}
		
		if (func[itr] != -1) nowFunc = func[itr];
	}
}
accuracy: 1
time_ms: 910
memory_kb: 68552
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

map<vi, int> id;

void init(int k)
{
	vi v;
	REP(i, k) v.push_back(i);
	int tmp = 0;
	do
	{
		id[v] = tmp;
		tmp++;
	} while (next_permutation(ALL(v)));
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	int n, k;
	cin >> n >> k;
	init(k);
	vector<vi> f(n, vi(k));
	REP(i, n) cin >> f[i];
	vector<vi> dag(n);
	vi in(n);
	int d; cin >> d;
	REP(i, d)
	{
		int a, b; cin >> a >> b;
		a--; b--;
		dag[a].push_back(b);
		in[b]++;
	}
	vector<priority_queue<pair<vi, int>>> que(id.size());
	REP(i, n)
	{
		if (in[i] == 0)
		{
			for (auto p : id)
			{
				vi pri;
				for (auto j : p.first)
				{
					pri.push_back(f[i][j]);
				}
				que[p.second].push(make_pair(pri, i));
			}
		}
	}
	vi e(k);
	REP(i, k)
	{
		cin >> e[i];
		e[i]--;
	}
	int r; cin >> r;
	int cnt = 0;
	int nex; cin >> nex;
	vector<bool> used(n);
	while (1)
	{
		int t = que[id[e]].top().second;
		que[id[e]].pop();
		if (used[t]) continue;
		used[t] = true;
		cout << t + 1 << endl;
		cnt++;

		for (auto i : dag[t])
		{
			in[i]--;
			if (in[i] == 0)
			{
				for (auto p : id)
				{
					vi pri;
					for (auto j : p.first)
					{
						pri.push_back(f[i][j]);
					}
					que[p.second].push(make_pair(pri, i));
				}
			}
		}

		if (cnt == n) break;
		if (cnt == nex)
		{
			REP(i, k)
			{
				cin >> e[i];
				e[i]--;
			}
			if (!(cin >> nex))
			{
				nex = 1e9;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 730
memory_kb: 82424
final_rank: 11

code_snippet: #include"bits/stdc++.h"
#include<random>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<endl;else cout<<"no"<<endl;
#define Yes(ans) if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define POSSIBLE(ans) if(ans)cout<<"POSSIBLE"<<endl;else cout<<"IMPOSSIBLE"<<endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9+7)
#define mod9 998244353
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(),x.end()
#define isin(i,n) (0<=i&&i<n)

int k;
vint tmp;

int main() {
	int fact[5] = { 1 };
	rep(i, 4)fact[i + 1] = fact[i] * (i + 1);
	int n;
	cin >> n >> k;
	tmp.resize(k);
	map<vint, int>perm;
	vvint order(fact[k], vint(n));
	{
		vvint f(n, vint(k + 1));
		rep(i, n)rep(j, k)cin >> f[i][j];
		rep(i, n)f[i][k] = i;
		rep(i, k)tmp[i] = i;
		int i = 0;
		do {
			sort(all(f), [](vint a, vint b) {
				rep(i, k) {
					if (a[tmp[i]] < b[tmp[i]])return true;
					if (a[tmp[i]] > b[tmp[i]])return false;
				}
				return true;
				});
			rep(j, n)order[i][f[j][k]] = j;
			perm[tmp] = i;
			i++;
		} while (next_permutation(all(tmp)));
	}
	int d;
	cin >> d;
	vvint g(n);
	vint indegree(n);
	rep(i, d) {
		int a, b;
		cin >> a >> b; a--; b--;
		g[a].push_back(b);
		indegree[b]++;
	}
	vector<priority_queue<pint>>que(fact[k]);
	vbool visited(n, false);
	rep(i, fact[k]) {
		rep(j, n) {
			if (indegree[j] == 0) {
				que[i].push({ order[i][j],j });
			}
		}
	}
	rep(i, k) {
		cin >> tmp[i]; tmp[i]--;
	}
	int nowperm = perm[tmp];
	int r;
	cin >> r;
	queue<pint> e;
	rep(i, r) {
		int m;
		cin >> m;
		rep(j, k) {
			cin >> tmp[j]; tmp[j]--;
		}
		e.push({ m,perm[tmp] });
	}

	int cnt = 0;
	while (cnt < n) {
		if (e.size() && e.front().first == cnt) {
			nowperm = e.front().second; e.pop();
		}
		auto [o, p] = que[nowperm].top(); que[nowperm].pop();
		if (visited[p])continue;
		visited[p] = true; cnt++;
		cout << p + 1 << endl;
		for (auto np : g[p]) {
			if (!(--indegree[np])) {
				rep(i, fact[k]) {
					que[i].push({ order[i][np],np });
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 21900
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const int INF = ((1<<30)-1);
const long long LINF = (1LL<<60);
const double PI = 3.141592653589793238;
template<class T> inline void chmax(T& a, T b) {if (a < b) {a = b;}}
template<class T> inline void chmin(T& a, T b) {if (a > b) {a = b;}}

vi g[100000];
vi need(100000);
vi cnt(100000);

int solve(){
    int n,k; cin >> n >> k;
    vector<vi> a(n , vi(k));
    rep(i,0,n)rep(j,0,k)cin >> a[i][j];
    int d; cin >> d;
    rep(i,0,d){
        int x,y; cin >> x >> y;
        --x; --y;
        g[x].push_back(y);
        need[y]++;
    }
    vector<vi> e(n);
    rep(i,0,k){
        int x; cin >> x;
        --x;
        e[0].push_back(x);
    }
    int r; cin >> r;
    rep(i,0,r){
        int m; cin >> m;
        rep(j,0,k){
           int x; cin >> x;
           --x;
           e[m].push_back(x);
        }
    }
    vi v(k);
    rep(i,0,k)v[i] = i;
    map<vi , vi> ord;
    int fac = 0;
    do
    {
        vector<vi> b(n);
        rep(i,0,n){
            rep(j,0,k)b[i].push_back(a[i][v[j]]);
            b[i].push_back(i);
        }
        sort(all(b));
        reverse(all(b));
        ord[v].resize(n);
        rep(i,0,n){
            ord[v][b[i].back()] = i;
        }
        fac++;
    } while (next_permutation(all(v)));
    vi can;
    rep(i,0,n)if(need[i] == 0)can.push_back(i);
    vi ans(n);
    vi now;
    map<vi , set<pi>> s;
    sort(all(v));
    do
    {
        rep(i,0,can.size()){
            s[v].insert(pi(ord[v][can[i]] , can[i]));
        }
    } while (next_permutation(all(v)));
    rep(i,0,n){
        if(e[i].size() != 0)now = e[i];
        pi tmp = *s[now].begin();
        int j = tmp.second;
        ans[i] = j;
        vi add;
        for(auto c:g[j]){
            cnt[c]++;
            if(cnt[c] == need[c])add.push_back(c);
        }
        sort(all(v));
        do
        {
            s[v].erase(pi(ord[v][j] , j));
            rep(l,0,add.size()){
                s[v].insert(pi(ord[v][add[l]] , add[l]));
            }
        } while (next_permutation(all(v)));
    }
    rep(i,0,n)cout << ans[i]+1 << endl;




    return 0;
}

int main(){
    cout << fixed << setprecision(10);
    ios::sync_with_stdio(0), cin.tie(0);
    int t = 1; 
    //cin >> t;
    for(int i = 0; i < t; i++){
        solve();
    }
	return 0;
}
accuracy: 1
time_ms: 1260
memory_kb: 72992
final_rank: 18

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0331.cc: スケジューラ
 */

#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 50000;

/* typedef */

typedef vector<int> vi;

struct Task {
  vi fs;
  int i;
  Task() {}
  Task(vi _fs, int _i): fs(_fs), i(_i) {}
  bool operator<(const Task &t) const { return fs > t.fs; }
};

/* global variables */

vi pss[24], fss[MAX_N][24], nbrs[MAX_N];
int pns[MAX_N], ms[MAX_N], sls[MAX_N], as[MAX_N];
set<Task> tss[24];

/* subroutines */

void readv(int k, vi &v) {
  v.resize(k);
  for (int i = 0; i < k; i++) scanf("%d", &v[i]), v[i]--;
}

void pushtask(int l, int u) {
  for (int i = 0; i < l; i++)
    tss[i].insert(Task(fss[u][i], u));
}

void erasetask(int l, int u) {
  for (int i = 0; i < l; i++)
    tss[i].erase(Task(fss[u][i], u));
}

/* main */

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  int n, k;
  scanf("%d%d", &n, &k);

  vi ps(k);
  for (int i = 0; i < k; i++) ps[i] = i;

  int l = 0;
  do {
    pss[l++] = ps;
  } while (next_permutation(ps.begin(), ps.end()));
  sort(pss, pss + l);
  //printf("l=%d\n", l);

  for (int i = 0; i < n; i++) {
    vi fs;
    readv(k, fs);
    for (int j = 0; j < l; j++) {
      fss[i][j].resize(k);
      for (int h = 0; h < k; h++) fss[i][j][h] = fs[pss[j][h]];
    }
  }

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

  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
    pns[v]++;
  }

  vi es;
  readv(k, es);
  int sel = lower_bound(pss, pss + l, es) - pss;

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

  for (int i = 0; i < r; i++) {
    scanf("%d", ms + i);
    readv(k, es);
    sls[i] = lower_bound(pss, pss + l, es) - pss;
  }

  for (int u = 0; u < n; u++)
    if (pns[u] == 0) pushtask(l, u);
  
  for (int i = 0, cm = 0; ! tss[0].empty();) {
    Task t = *tss[sel].begin();
    int u = t.i;
    erasetask(l, u);

    as[cm++] = u;

    for (auto v: nbrs[u])
      if (--pns[v] == 0) pushtask(l, v);

    if (i < r && cm == ms[i]) sel = sls[i++];
  }

  for (int i = 0; i < n; i++) printf("%d\n", as[i] + 1);
  return 0;
}
accuracy: 1
time_ms: 1260
memory_kb: 201772
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N, K;
  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];
    }
  }
  int D;
  cin >> D;
  vector<vector<int>> E(N);
  for (int i = 0; i < D; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
  }
  vector<int> e(K);
  for (int i = 0; i < K; i++){
    cin >> e[i];
    e[i]--;
  }
  int R;
  cin >> R;
  vector<int> m(R);
  vector<vector<int>> e2(R, vector<int>(K));
  for (int i = 0; i < R; i++){
    cin >> m[i];
    for (int j = 0; j < K; j++){
      cin >> e2[i][j];
      e2[i][j]--;
    }
  }
  vector<int> d(N, 0);
  for (int i = 0; i < N; i++){
    for (int j : E[i]){
      d[j]++;
    }
  }
  vector<int> p(K);
  for (int i = 0; i < K; i++){
    p[i] = i;
  }
  vector<vector<int>> P;
  while (true){
    P.push_back(p);
    if (!next_permutation(p.begin(), p.end())){
      break;
    }
  }
  int F = P.size();
  vector<vector<int>> pr(F, vector<int>(N));
  for (int i = 0; i < F; i++){
    vector<pair<vector<int>, int>> P2(N);
    for (int j = 0; j < N; j++){
      vector<int> f2(K);
      for (int k = 0; k < K; k++){
        f2[k] = f[j][P[i][k]];
      }
      P2[j] = make_pair(f2, j);
    }
    sort(P2.begin(), P2.end());
    for (int j = 0; j < N; j++){
      pr[i][P2[j].second] = j;
    }
  }
  vector<priority_queue<pair<int, int>>> pq(F);
  for (int i = 0; i < F; i++){
    for (int j = 0; j < N; j++){
      if (d[j] == 0){
        pq[i].push(make_pair(pr[i][j], j));
      }
    }
  }
  vector<int> upd(N, -1);
  for (int i = 0; i < R; i++){
    upd[m[i]] = i;
  }
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++){
    if (upd[i] != -1){
      e = e2[upd[i]];
    }
    int id = lower_bound(P.begin(), P.end(), e) - P.begin();
    while (used[pq[id].top().second]){
      pq[id].pop();
    }
    int v = pq[id].top().second;
    cout << v + 1 << endl;
    pq[id].pop();
    used[v] = true;
    for (int w : E[v]){
      d[w]--;
      if (d[w] == 0){
        for (int j = 0; j < F; j++){
          pq[j].push(make_pair(pr[j][w], w));
        }
      }
    }
  }
}
accuracy: 1
time_ms: 430
memory_kb: 27724
final_rank: 6

code_snippet: #include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
int N,K;
int E[50000];
int M[50000];
int Eid;
map<vector<int>,int>mp;
vector<vector<int> >ord;
int erd()
{
	vector<int>A(K);
	for(int i=0;i<K;i++)
	{
		cin>>A[i];
		A[i]--;
	}
	if(mp.find(A)==mp.end())
	{
		mp[A]=ord.size();
		ord.push_back(A);
	}
	return mp[A];
}
struct dat{
	int id;
	vector<int>F;
	void rd(){
		F.assign(K,0);
		for(int i=0;i<K;i++)cin>>F[i];
	}
	bool operator<(const dat&rhs)const{
		return F<rhs.F;
	}
};
dat A[50000];
void psh(dat a,priority_queue<dat>&P,int id)
{
	dat b;
	b.id=a.id;
	vector<int>F(K);
	for(int i=0;i<K;i++)
	{
		F[i]=a.F[ord[id][i]];
	}
	b.F=F;
	P.push(b);
}
vector<int>G[50000];
int indeg[50000];
bool out[50001];
main()
{
	cin>>N>>K;
	for(int i=0;i<N;i++)
	{
		A[i].rd();
		A[i].id=i+1;
	}
	int D;cin>>D;
	for(;D--;D)
	{
		int a,b;cin>>a>>b;
		G[a-1].push_back(b-1);
		indeg[b-1]++;
	}
	E[0]=erd();
	int R;cin>>R;
	for(int i=1;i<=R;i++)
	{
		cin>>M[i];
		E[i]=erd();
	}
	priority_queue<dat>P[ord.size()];
	for(int i=0;i<N;i++)if(!indeg[i])
	{
		for(int j=0;j<ord.size();j++)psh(A[i],P[j],j);
	}
	for(int i=0;i<N;i++)
	{
		if(Eid<R&&i==M[Eid+1])Eid++;
		int u;
		while(true)
		{
			u=P[E[Eid]].top().id;
			P[E[Eid]].pop();
			if(!out[u])break;
		}
		out[u]=true;
		cout<<u<<endl;
		for(int v:G[u-1])if(!--indeg[v])
		{
			for(int j=0;j<ord.size();j++)psh(A[v],P[j],j);
		}
	}
}
accuracy: 1
time_ms: 970
memory_kb: 83036
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<array<int, 4>> perms;
vector<array<int, 4>> v;
struct P {
    int id, x;
    P(int id = 0, int x = 0) : id(id), x(x) {};

    bool operator<(const P &b) const {
        for (auto &&i : perms[x]) {
            if(v[id][i] != v[b.id][i]) return v[id][i] < v[b.id][i];
        }
        return false;
    }
};

int main() {
    int n, k;
    cin >> n >> k;
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < k; ++j) {
            scanf("%d", &v[i][j]);
        }
    }
    int d;
    cin >> d;
    vector<vector<int>> G(n);
    vector<int> deg(n);
    for (int i = 0; i < d; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        deg[b]++;
        G[a].emplace_back(b);
    }
    {
        array<int, 4> a{};
        iota(a.begin(),a.begin()+k, 0);
        do {
            perms.emplace_back(a);
        }while (next_permutation(a.begin(),a.begin()+k));
    }
    int m = perms.size();
    vector<priority_queue<P>> Q(m);
    vector<int> visited(n);
    for (int i = 0; i < n; ++i) {
        if(!deg[i]){
            for (int j = 0; j < m; ++j) {
                Q[j].emplace(i, j);
            }
        }
    }
    int p = 0;
    auto update = [&](array<int, 4> &v){
        for (int i = 0; i < perms.size(); ++i) {
            if(v == perms[i]) {
                p = i;
                return;
            }
        }
    };
    {
        array<int, 4> x{};
        for (int i = 0; i < k; ++i) {
            cin >> x[i];
            x[i]--;
        }
        update(x);
    }
    int q;
    cin >> q;
    vector<pair<int, array<int, 4>>> query(q);
    for (int i = 0; i < q; ++i) {
        scanf("%d", &query[i].first);
        for (int j = 0; j < k; ++j) {
            scanf("%d", &query[i].second[j]);
            query[i].second[j]--;
        }
    }
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        while(cur < q && query[cur].first == i){
            update(query[cur++].second);
        }
        int a = Q[p].top().id; Q[p].pop();
        while(visited[a]){
            a = Q[p].top().id; Q[p].pop();
        }
        printf("%d\n", a+1);
        visited[a] = 1;
        for (auto &&j : G[a]) {
            if(!--deg[j]){
                for (int l = 0; l < m; ++l) {
                    Q[l].emplace(j, l);
                }
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 17412
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
const ll LINF=0x1fffffffffffffff;
#define _name3(_1,_2,_3,name,...) name
#define _rep1(n) for(ll i=0;i<(n);++i)
#define _rep2(i,n) for(ll i=0;i<(n);++i)
#define _rep3(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(...) _name3(__VA_ARGS__,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define rrep(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define each(i,a) for(auto &i:a)
#define sum(a) accumulate(range(a),0LL)
#define range(i) (i).begin(),(i).end()
template<class T> inline constexpr bool chmin(T &mn,const T &cnt){if(mn>cnt){mn=cnt;return 1;}else return 0;}
template<class T> inline constexpr bool chmax(T &mx,const T &cnt){if(mx<cnt){mx=cnt;return 1;}else return 0;}


int n,k;
vector<vector<int>>f,g;
vector<bool>used;
struct pq{
    const vector<int>e;
    vector<int>waiting;
    priority_queue<int,vector<int>,function<bool(int,int)>>q;
    pq(const vector<int>&e,const vector<int>&waiting):e(e),waiting(waiting),q([&e](int a,int b)->bool{for(auto&i:e){if(f[a].at(i)!=f[b].at(i))return f[a][i]<f[b][i];}return 0;}){
        rep(n)if(!waiting[i])q.push(i);
    }
    int pop(){
        int ans;
        do{
            ans=q.top();
            q.pop();
        }while(used.at(ans));
        used.at(ans)=1;
        return ans;
    }
    void push(int x){
        q.push(x);
    }
};
int main() {
    cin>>n>>k;
    f.assign(n,vector<int>(k));
    for(auto&i:f)for(auto&j:i)cin>>j;
    int d;
    cin>>d;
    g.resize(n);
    vector<int>waiting(n);
    rep(d){
        int a,b;
        cin>>a>>b;
        a--;b--;
        g[a].push_back(b);
        waiting[b]++;
    }
    unordered_map<vector<int>,int,function<unsigned(const vector<int>&)>>m(100,[](const vector<int>&e)->unsigned{unsigned ans=0;each(i,e){ans<<=3;ans|=i;}return ans;});
    vector<int>e(k);
    iota(range(e),0);
    vector<pq>q;
    int cnt=0;
    do{
        m[e]=cnt++;
        q.push_back({m.find(e)->first,waiting});
    }while(next_permutation(range(e)));
    vector<int>ans;
    each(i,e){
        cin>>i;
        i--;
    }
    int E=m[e];
    cnt=0;
    int r;
    cin>>r;
    vector<pll>es(r);
    each(i,es){
        cin>>i.first;
        each(j,e){
            cin>>j;
            j--;
        }
        i.second=m[e];
    }
    reverse(range(es));
    used.resize(n);
    each(i,q)each(j,i.e)assert(0<=j&&j<k);
    while(cnt<n){
        if(es.size()&&cnt==es.back().first){
            E=es.back().second;
            es.pop_back();
        }
        while(true){
            int a=q[E].pop();
            each(i,g[a])if(!--waiting[i]){
                each(j,q){
                    j.push(i);
                }
            }
            ans.push_back(a+1);
            break;
        }
        cnt++;
    }
    each(i,ans)cout<<i<<endl;
}
accuracy: 1
time_ms: 580
memory_kb: 20956
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
struct Attributes {
	int id;
	std::vector<int> parameters;
};
class PriorityQueueLike {
	std::vector<int> rank;
	std::priority_queue<int, std::vector<int>, std::function<bool(const int, const int)>> queue;
public:
	PriorityQueueLike(std::vector<int>&& _rank) : rank{ std::move(_rank) } {
		queue = std::priority_queue<int, std::vector<int>, std::function<bool(const int, const int)>>([&](const int a, const int b) {return rank[a] < rank[b]; });
	}
	void push(int id) {
		queue.push(id);
	}
};
int factorial(const int n) {
	switch (n) {
	case 0: return 1;
	case 1: return 1;
	default: return n * factorial(n - 1);
	}
}
void set_ranks(std::vector<int> current_rank, int current, int count, int state, std::vector<int> &history, std::vector<std::pair<int, std::vector<int>>>& result, const std::vector<Attributes>& attr) {
	std::stable_sort(current_rank.begin(), current_rank.end(), [&attr, current](const int a, const int b) {return attr[a].parameters[current] > attr[b].parameters[current]; });
	if (state + 1 == (1 << count)) {
		int as_int = 0;
		for (auto i = 1; i <= history.size(); ++i) {
			as_int = as_int * 10 + history[history.size() - i];
		}
		std::vector<int> rank(current_rank.size());
		for (auto i = 0; i < current_rank.size(); ++i) {
			rank[current_rank[i]] = i;
		}
		result.push_back(std::make_pair(as_int, std::move(rank)));
	}
	else {
		for (auto next = 0; next < count; ++next) if ((state & (1 << next)) == 0) {
			history.push_back(next);
			set_ranks(current_rank, next, count, state | (1 << next), history, result, attr);
			history.pop_back();
		}
	}
}
int main()
{
	int n, k; std::cin >> n >> k;
	std::vector<Attributes> tasks(n);
	for (auto i = 0; i < n; ++i) {
		tasks[i].id = i;
		tasks[i].parameters.resize(k);
		for (auto& f : tasks[i].parameters) std::cin >> f;
	}
	std::vector<std::pair<int, std::vector<int>>> ranks; ranks.reserve(factorial(k));
	std::unordered_map<int, std::priority_queue<int, std::vector<int>, std::function<bool(const int a, const int b)>>> queues;
	{
		std::vector<int> vec(n), history; std::iota(vec.begin(), vec.end(), 0);
		for (auto i = 0; i < k; ++i) {
			history.push_back(i);
			set_ranks(vec, i, k, 1 << i, history, ranks, tasks);
			history.pop_back();
		}
		for (const auto& pair : ranks) {
			queues[pair.first] = std::priority_queue<int, std::vector<int>, std::function<bool(const int a, const int b)>>([&pair](const int a, const int b) {return pair.second[a] > pair.second[b]; });
		}
	}
	std::vector<std::vector<int>> next_task(n);
	std::vector<int> prev_count(n, 0);
	{
		int d; std::cin >> d;
		for (auto i = 0; i < d; ++i) {
			int a, b; std::cin >> a >> b; --a; --b;
			next_task[a].push_back(b);
			prev_count[b]++;
		}
	}
	for (auto& pair : queues) {
		for (auto i = 0; i < n; ++i) if (prev_count[i] == 0) {
			pair.second.push(i);
		}
	}
	std::vector<bool> executed(n, false);
	std::vector<int> result; result.reserve(n);
	int priority = 0;
	for (auto i = 0; i < k; ++i) {
		int e; std::cin >> e;
		priority = priority * 10 + e - 1;
	}
	auto queue = &queues[priority];
	int r; std::cin >> r;
	int last = 0;
	for (auto i = 0; i < r; ++i) {
		int m; std::cin >> m;
		priority = 0;
		for (auto j = 0; j < k; ++j) {
			int e; std::cin >> e;
			priority = priority * 10 + e - 1;
		}
		for (; last < m; ++last) {
			while (executed[queue->top()]) queue->pop();
			auto task = queue->top();
			executed[task] = true;
			result.push_back(task);
			if (!next_task[task].empty()) {
				for (const auto next : next_task[task]) {
					--prev_count[next];
				}
				for (auto& pair : queues) {
					for (const auto next : next_task[task]) if (prev_count[next] == 0) {
						pair.second.push(next);
					}
				}
			}
		}
		queue = &queues[priority];
	}
	for (; last < n; ++last) {
		while (executed[queue->top()]) queue->pop();
		auto task = queue->top();
		executed[task] = true;
		result.push_back(task);
		if (!next_task[task].empty()) {
			for (const auto next : next_task[task]) {
				if (--prev_count[next] == 0) queue->push(next);
			}
		}
	}
	for (const auto r : result) std::cout << r + 1 << '\n';
}
accuracy: 1
time_ms: 830
memory_kb: 17568
final_rank: 9

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int perm_input(int K, vector<vector<int> > &perm) {
	vector<int> p(K);
	for (int i = 0; i < K; ++i) cin >> p[i], --p[i];
	return lower_bound(perm.begin(), perm.end(), p) - perm.begin();
}
int main() {
	int N, K;
	cin >> N >> K;
	vector<vector<int> > perm;
	vector<int> pperm(K);
	for (int i = 0; i < K; ++i) pperm[i] = i;
	do {
		perm.push_back(pperm);
	} while (next_permutation(pperm.begin(), pperm.end()));
	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];
		}
	}
	int fact = perm.size();
	vector<vector<int> > P(fact, vector<int>(N)), IP(fact, vector<int>(N));
	for (int i = 0; i < fact; ++i) {
		for (int j = 0; j < N; ++j) {
			P[i][j] = j;
		}
		sort(P[i].begin(), P[i].end(), [&](int x, int y) {
			for (int j = 0; j < K; ++j) {
				if (F[x][perm[i][j]] != F[y][perm[i][j]]) {
					return F[x][perm[i][j]] < F[y][perm[i][j]];
				}
			}
			return false;
			});
		for (int j = 0; j < N; ++j) {
			IP[i][P[i][j]] = j;
		}
	}
	int D;
	cin >> D;
	vector<vector<int> > G(N);
	vector<int> deg(N);
	for (int i = 0; i < D; ++i) {
		int a, b;
		cin >> a >> b; --a, --b;
		++deg[b];
		G[a].push_back(b);
	}
	int cur = 0, pre = perm_input(K, perm), R;
	vector<int> ord(N + 1);
	cin >> R;
	for (int i = 0; i < R; ++i) {
		int tm, nxt;
		cin >> tm;
		nxt = perm_input(K, perm);
		for (int j = cur; j < tm; ++j) {
			ord[j] = pre;
		}
		cur = tm;
		pre = nxt;
	}
	for (int i = cur; i <= N; ++i) {
		ord[i] = pre;
	}
	vector<priority_queue<int> > que(fact);
	for (int i = 0; i < N; ++i) {
		if (deg[i] >= 1) continue;
		for (int j = 0; j < fact; ++j) {
			que[j].push(IP[j][i]);
		}
	}
	vector<bool> used(N, false);
	for (int i = 0; i < N; ++i) {
		int u = -1;
		while (u == -1 || used[u]) {
			u = P[ord[i]][que[ord[i]].top()];
			que[ord[i]].pop();
		}
		used[u] = true;
		cout << u + 1 << '\n';
		for (int j : G[u]) {
			--deg[j];
			if (deg[j] == 0) {
				for (int k = 0; k < fact; ++k) {
					que[k].push(IP[k][j]);
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 22568
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef pair<vector<int>, int> P;

priority_queue<P> pq[20];

int f[50010][4];

map<vector<int>, int> mp;
vector<vector<int>> zyun;
int n, k;
void load(int x){
	for(int i = 0;i < zyun.size();i++){
		vector<int> tmp;
		for(int j = 0;j < k;j++){
			tmp.push_back(f[x][zyun[i][j]]);
		}
		pq[i].push({tmp, x});
	}
}

vector<int> graph[50010];
int ydk[50010];

vector<int> e[50010];
int m[50010];

int used[50010];


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> k;


	{
		vector<int> vec(k);
		for(int i = 0;i < k;i++){
			vec[i] = i;
		}
		
		int cnt = 0;
		do{
			zyun.push_back(vec);
			mp[vec] = cnt;
			cnt++;
		}while(next_permutation(vec.begin(), vec.end()));
	}

	for(int x = 0;x < n;x++){
		for(int j = 0;j < k;j++){
			cin >> f[x][j];
		}
	}

	int d;
	cin >> d;

	for(int i = 0;i < d;i++){
		int a, b; cin >> a >> b;
		a--; b--;
		graph[a].push_back(b);
		ydk[b]++;
	}

	int r;
	for(int i = 0;i < k;i++){
		int t; cin >> t;
		e[0].push_back(t-1);
	}

	cin >> r;
	for(int j = 0;j < r;j++){
		cin >> m[j+1];
		for(int i = 0;i < k;i++){
			int t; cin >> t;
			e[j+1].push_back(t-1);
		}
	}
	r++;
	m[r] = n;

	for(int i = 0;i < n;i++){
		if(ydk[i] == 0) load(i);
	}

	int now = mp[e[0]];

	vector<int> ans;
	int fi = 0;
	int p = 0;
	for(int x = 0;x < n;x++){
		int idx;
		while(1){
			idx = pq[now].top().second;
			pq[now].pop();
			if(used[idx] == 0){
				break;
			}
		}

		ans.push_back(idx+1);
		used[idx] = 1;

		for(auto p : graph[idx]){
			ydk[p]--;
			if(ydk[p] == 0){
				load(p);
			}
		}

		fi++;
		if(m[p+1] == fi){
			p++;
			now = mp[e[p]];
		}
	}

	for(auto p : ans){
		cout << p << endl;
	}

		
	return 0;
}
accuracy: 1
time_ms: 1030
memory_kb: 83732
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef pair<vector<int>, int> P;

priority_queue<P> pq[20];

int f[50010][4];

map<vector<int>, int> mp;
vector<vector<int>> zyun;
int n, k;
void load(int x){
	for(int i = 0;i < zyun.size();i++){
		vector<int> tmp;
		for(int j = 0;j < k;j++){
			tmp.push_back(f[x][zyun[i][j]]);
		}
		pq[i].push({tmp, x});
	}
}

vector<int> graph[50010];
int ydk[50010];

vector<int> e[50010];
int m[50010];

int used[50010];


int main(){
	cin >> n >> k;


	{
		vector<int> vec(k);
		for(int i = 0;i < k;i++){
			vec[i] = i;
		}
		
		int cnt = 0;
		do{
			zyun.push_back(vec);
			mp[vec] = cnt;
			cnt++;
		}while(next_permutation(vec.begin(), vec.end()));
	}

	for(int x = 0;x < n;x++){
		for(int j = 0;j < k;j++){
			cin >> f[x][j];
		}
	}

	int d;
	cin >> d;

	for(int i = 0;i < d;i++){
		int a, b; cin >> a >> b;
		a--; b--;
		graph[a].push_back(b);
		ydk[b]++;
	}

	int r;
	for(int i = 0;i < k;i++){
		int t; cin >> t;
		e[0].push_back(t-1);
	}

	cin >> r;
	for(int j = 0;j < r;j++){
		cin >> m[j+1];
		for(int i = 0;i < k;i++){
			int t; cin >> t;
			e[j+1].push_back(t-1);
		}
	}
	r++;
	m[r] = n;

	for(int i = 0;i < n;i++){
		if(ydk[i] == 0) load(i);
	}

	int now = mp[e[0]];

	vector<int> ans;
	int fi = 0;
	int p = 0;
	for(int x = 0;x < n;x++){
		int idx;
		while(1){
			idx = pq[now].top().second;
			pq[now].pop();
			if(used[idx] == 0){
				break;
			}
		}

		ans.push_back(idx+1);
		used[idx] = 1;

		for(auto p : graph[idx]){
			ydk[p]--;
			if(ydk[p] == 0){
				load(p);
			}
		}

		fi++;
		if(m[p+1] == fi){
			p++;
			now = mp[e[p]];
		}
	}

	for(auto p : ans){
		cout << p << endl;
	}

		
	return 0;
}
accuracy: 1
time_ms: 1050
memory_kb: 83620
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, k;

typedef struct{
  int num;
  int data[4];
} Syori;



queue<int> graph[50050];
int izon[50050];
Syori data[50050];

Syori syori[24] = {};
int syoriTable[4][4][4][4];
Syori syoritmp;
int scnt = 0;

int make(int i, int bit){
  if(i == 4){
    syori[scnt] = syoritmp;
    syoriTable[syoritmp.data[0]][syoritmp.data[1]][syoritmp.data[2]][syoritmp.data[3]] = scnt;
    scnt++;
  }

  for(int j = 0;j < 4;j++){
    if((bit & (1 << j)) == 0){
      syoritmp.data[i] = j;
      make(i+1,bit | (1 << j));
    }
  }

  return 0;
}

Syori f[50050];


Syori segTree[24][200000] = {};
int segSize;

bool big(Syori a, Syori b, int i){
  if(a.data[syori[i].data[0]] == b.data[syori[i].data[0]]){
    if(a.data[syori[i].data[1]] == b.data[syori[i].data[1]]){
      if(a.data[syori[i].data[2]] == b.data[syori[i].data[2]]){
        return a.data[syori[i].data[3]] > b.data[syori[i].data[3]];
      }else{
        return a.data[syori[i].data[2]] > b.data[syori[i].data[2]];
      }
    }else{
      return a.data[syori[i].data[1]] > b.data[syori[i].data[1]];
    }
  }else{
    return a.data[syori[i].data[0]] > b.data[syori[i].data[0]];
  }
}

Syori nu;

void init(){
  segSize = 1;
  while(segSize < n){
    segSize *= 2;
  }

  nu.data[0] = 0;
  nu.data[1] = 0;
  nu.data[2] = 0;
  nu.data[3] = 0;

  for(int i = 0;i < 24;i++){
    for(int j = 0;j < segSize;j++){
      if(j < n && izon[j] == 0){
        segTree[i][segSize-1+j] = f[j];
        segTree[i][segSize-1+j].num = j;
      }else{
        segTree[i][segSize-1+j] = nu;
        segTree[i][segSize-1+j].num = j;
      }
    }
  }

  for(int i = 0;i < 24;i++){
    for(int j = segSize-2;j >= 0;j--){
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }

  return;
}

void insert(int x){

  int j;

  for(int i = 0;i < 24;i++){
    j = segSize-1+x;
    segTree[i][j] = f[x];
    segTree[i][j].num = x;
    while(j > 0){
      j = (j - 1) /2;
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }
}


void del(int x){
  int j;
  for(int i = 0;i < 24;i++){
    j = segSize-1+x;
    segTree[i][j] = nu;
    while(j > 0){
      j = (j - 1) /2;
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }
  return;
}

int getData(int i){
  return segTree[i][0].num;
}


int main(){
  cin >> n >> k;

  for(int i = 0;i < n;i++){
    for(int j = 0;j < 4;j++){
      if(j < k){
        cin >> f[i].data[j];
      }else{
        f[i].data[j] = 0;
      }
    }
  }

  int d; cin >> d;
  for(int i = 0;i < d;i++){
    int a, b;
    cin >> a >> b;
    a--; b--;
    izon[b]++;
    graph[a].push(b);
  }

  for(int i = 0;i < 4;i++){
    if(i < k){
      cin >> data[0].data[i];
      data[0].data[i]--;
    }else{
      data[0].data[i] = i;
    }
  }

  int r;
  cin >> r;
  int m;
  int tmp = 0;

  for(int i = 0;i < r;i++){
    cin >> m;
    for(int j = tmp+1;j < m;j++){
      data[j] = data[j-1];
    }
    for(int j = 0;j < 4;j++){
      if(j < k){
        cin >> data[m].data[j];
        data[m].data[j]--;
      }else{
        data[m].data[j] = j;
      }
    }
    tmp = m;
  }

  for(int j = tmp+1;j < n;j++){
    data[j] = data[j-1];
  }

  make(0,0);

  init();

  queue<int> ans;
  for(int i = 0;i < n;i++){
    int tmpi = getData(syoriTable[data[i].data[0]][data[i].data[1]][data[i].data[2]][data[i].data[3]]);
    ans.push(tmpi);
    del(tmpi);
    while(!graph[tmpi].empty()){
      izon[graph[tmpi].front()]--;
      if(izon[graph[tmpi].front()] == 0){
        insert(graph[tmpi].front());
      }
      graph[tmpi].pop();
    }
  }

  for(int i = 0;i < n;i++){
    cout << ans.front()+1 << endl;
    ans.pop();
  }
  return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 100484
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, k;

typedef struct{
  int num;
  int data[4];
} Syori;



queue<int> graph[50050];
int izon[50050];
Syori data[50050];

Syori syori[24] = {};
int syoriTable[4][4][4][4];
Syori syoritmp;
int scnt = 0;

int make(int i, int bit){
  if(i == 4){
    syori[scnt] = syoritmp;
    syoriTable[syoritmp.data[0]][syoritmp.data[1]][syoritmp.data[2]][syoritmp.data[3]] = scnt;
    scnt++;
  }

  for(int j = 0;j < 4;j++){
    if((bit & (1 << j)) == 0){
      syoritmp.data[i] = j;
      make(i+1,bit | (1 << j));
    }
  }

  return 0;
}

Syori f[50050];


Syori segTree[24][200000] = {};
int segSize;

bool big(Syori a, Syori b, int i){
  if(a.data[syori[i].data[0]] == b.data[syori[i].data[0]]){
    if(a.data[syori[i].data[1]] == b.data[syori[i].data[1]]){
      if(a.data[syori[i].data[2]] == b.data[syori[i].data[2]]){
        return a.data[syori[i].data[3]] > b.data[syori[i].data[3]];
      }else{
        return a.data[syori[i].data[2]] > b.data[syori[i].data[2]];
      }
    }else{
      return a.data[syori[i].data[1]] > b.data[syori[i].data[1]];
    }
  }else{
    return a.data[syori[i].data[0]] > b.data[syori[i].data[0]];
  }
}

Syori nu;

void init(){
  segSize = 1;
  while(segSize < n){
    segSize *= 2;
  }

  nu.data[0] = 0;
  nu.data[1] = 0;
  nu.data[2] = 0;
  nu.data[3] = 0;

  for(int i = 0;i < 24;i++){
    for(int j = 0;j < segSize;j++){
      if(j < n && izon[j] == 0){
        segTree[i][segSize-1+j] = f[j];
        segTree[i][segSize-1+j].num = j;
      }else{
        segTree[i][segSize-1+j] = nu;
        segTree[i][segSize-1+j].num = j;
      }
    }
  }

  for(int i = 0;i < 24;i++){
    for(int j = segSize-2;j >= 0;j--){
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }

  return;
}

void insert(int x){

  int j;

  for(int i = 0;i < 24;i++){
    j = segSize-1+x;
    segTree[i][j] = f[x];
    segTree[i][j].num = x;
    while(j > 0){
      j = (j - 1) /2;
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }
}


void del(int x){
  int j;
  for(int i = 0;i < 24;i++){
    j = segSize-1+x;
    segTree[i][j] = nu;
    while(j > 0){
      j = (j - 1) /2;
      if(big(segTree[i][j*2+1],segTree[i][j*2+2],i)){
        segTree[i][j] = segTree[i][j*2+1];
      }else{
        segTree[i][j] = segTree[i][j*2+2];
      }
    }
  }
  return;
}

int getData(int i){
  return segTree[i][0].num;
}


int main(){
  cin >> n >> k;

  for(int i = 0;i < n;i++){
    for(int j = 0;j < 4;j++){
      if(j < k){
        cin >> f[i].data[j];
      }else{
        f[i].data[j] = 0;
      }
    }
  }

  int d; cin >> d;
  for(int i = 0;i < d;i++){
    int a, b;
    cin >> a >> b;
    a--; b--;
    izon[b]++;
    graph[a].push(b);
  }

  for(int i = 0;i < 4;i++){
    if(i < k){
      cin >> data[0].data[i];
      data[0].data[i]--;
    }else{
      data[0].data[i] = i;
    }
  }

  int r;
  cin >> r;
  int m;
  int tmp = 0;

  for(int i = 0;i < r;i++){
    cin >> m;
    for(int j = tmp+1;j < m;j++){
      data[j] = data[j-1];
    }
    for(int j = 0;j < 4;j++){
      if(j < k){
        cin >> data[m].data[j];
        data[m].data[j]--;
      }else{
        data[m].data[j] = j;
      }
    }
    tmp = m;
  }

  for(int j = tmp+1;j < n;j++){
    data[j] = data[j-1];
  }

  make(0,0);

  init();

  queue<int> ans;
  for(int i = 0;i < n;i++){
    int tmpi = getData(syoriTable[data[i].data[0]][data[i].data[1]][data[i].data[2]][data[i].data[3]]);
    ans.push(tmpi);
    del(tmpi);
    while(!graph[tmpi].empty()){
      izon[graph[tmpi].front()]--;
      if(izon[graph[tmpi].front()] == 0){
        insert(graph[tmpi].front());
      }
      graph[tmpi].pop();
    }
  }

  for(int i = 0;i < n;i++){
    cout << ans.front()+1 << endl;
    ans.pop();
  }
  return 0;
}
accuracy: 1
time_ms: 720
memory_kb: 100628
final_rank: 14

code_snippet: #include <stdio.h>
#include <queue>
#include <bitset>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
#define PB push_back
int t=0,k;
struct node{
    int num;
    int val[4];
    void in(){
        num=++t;
        for(int i=0;i<k;i++)scanf("%d",&val[i]);
        return ;
    }
    node per(vector<int>& p){
        node ans;
        ans.num=num;
        for(int i=0;i<k;i++)ans.val[i]=val[p[i]];
        return ans;
    }
};
struct cmp{
    bool operator()(const node a,const node b){
        for(int i=0;i<k;i++)if(a.val[i]!=b.val[i])return a.val[i]<b.val[i];
        return true;
    }
};
const int N=5e4+10;
int main(){
    int n,tot=1,d,l,r,deg[N],now=1,last,cnt=0,temp;
    map<vector<int>,int>m;
    map<int,vector<int>>b;
    node a[N],nxt;
    vector<int> p,graph[N],tp;
    bitset<N>used;
    priority_queue<node,vector<node>,cmp>pq[24];
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)a[i].in();
    for(int i=0;i<k;i++)p.PB(i);
    for(int i=k;i>0;i--)tot*=i;
    for(int i=0;i<tot;i++){
        m.insert({p,i});
        b.insert({i,p});
        next_permutation(p.begin(),p.end());
    }
    used.reset();
    scanf("%d",&d);
    while(d--){
        scanf("%d%d",&l,&r);
        graph[l].PB(r);
        deg[r]++;
    }
    for(int i=1;i<=n;i++)if(deg[i]==0)for(int j=0;j<tot;j++)pq[j].push(a[i].per(b[j]));
    tp.clear();
    for(int i=0;i<k;i++){
        scanf("%d",&temp);
        temp--;
        tp.PB(temp);
    }
    now=m[tp];
    scanf("%d",&r);
    while(r--){
        last=now;
        scanf("%d",&l);
        tp.clear();
        for(int i=0;i<k;i++){
            scanf("%d",&temp);
            temp--;
            tp.PB(temp);
        }
        now=m[tp];
        while(cnt<l){
            nxt=pq[last].top();
            pq[last].pop();
            if(used[nxt.num])continue;
            used[nxt.num]=true;
            printf("%d\n",nxt.num);
            for(int i:graph[nxt.num]){
                deg[i]--;
                if(deg[i]==0)for(int j=0;j<tot;j++)pq[j].push(a[i].per(b[j]));
            }
            cnt++;
        }
    }
    while(cnt<n){
        nxt=pq[now].top();
        pq[now].pop();
        if(used[nxt.num])continue;
        used[nxt.num]=true;
        printf("%d\n",nxt.num);
        for(int i:graph[nxt.num]){
            deg[i]--;
            if(deg[i]==0)for(int j=0;j<tot;j++)pq[j].push(a[i].per(b[j]));
        }
        cnt++;
    }
}
accuracy: 1
time_ms: 350
memory_kb: 29604
final_rank: 3

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
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;
}
accuracy: 1
time_ms: 350
memory_kb: 16396
final_rank: 2

code_snippet: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M, K, R;
int F[50000][4];
vector<int> G[50000];
int deg[50000];
int ord[50000][4];
vector<int> vect(vector<int> ord, int i) {
  vector<int> ret;
  rep(j, K) ret.pb(F[i][ord[j]]);
  ret.pb(i);
  return ret;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> K;
  rep(i, N) rep(j, K) cin >> F[i][j], F[i][j] = -F[i][j];
  cin >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].pb(b);
    deg[b]++;
  }
  int last = 0;
  rep(j, K) cin >> ord[0][j], ord[0][j]--;
  cin >> R;
  rep(_, R) {
    int m;
    cin >> m;
    for (int p=last+1; p<m; p++) {
      rep(j, K) ord[p][j] = ord[p-1][j];
    }
    rep(j, K) cin >> ord[m][j], ord[m][j]--;
    last = m;
  }
  for (int p=last+1; p<N; p++) {
    rep(j, K) ord[p][j] = ord[p-1][j];
  }

  map<vector<int>, set<vector<int> > > mp;
  vector<int> r(K);
  rep(i, K) r[i] = i;
  do {
    set<vector<int> > vs;
    mp[r] = vs;
  }
  while (next_permutation(all(r)));

  rep(i, N) {
    if (deg[i] == 0) {
      for (auto &p : mp) p._2.insert(vect(p._1, i));
    }
  }

  rep(p, N) {
    vector<int> r(K); rep(i, K) r[i] = ord[p][i];
    assert(!mp[r].empty());
    auto &vs = *mp[r].begin();
    int x = vs.back();
    for (int t : G[x]) {
      deg[t]--;
      if (deg[t] == 0) {
        for (auto &p : mp) {
          p._2.insert(vect(p._1, t));
        }
      }
    }
    for (auto &p : mp) {
      p._2.erase(vect(p._1, x));
    }
    cout << x+1 << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 1790
memory_kb: 137272
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
struct Data
{
    int data[4];
    int num;
 
    Data () {}
    Data (int a, int b, int c, int d, int n)
    {
        data[0] = a, data[1] = b, data[2] = c, data[3] = d, num = n;
    }
 
    bool operator < (const Data d) const
    {
        for(int i = 0; i < 4; i++) {
            if(data[i] < d.data[i]) return (true);
            else if(data[i] > d.data[i]) return (false);
        }
        return (false);
    }
};
 
int N, K;
int F[50005][4];
int D, R;
bool flag[50005] = {};
int quant[50005] = {};
vector < int > G[50005];
vector < int > order;
int it = 0;
int idx = 1;
vector < int > pat[24];
priority_queue < Data > pq[24];
map < vector < int > , int > mp;
 
void process(int m);
 
int main()
{
 
 
    cin >> N >> K;
 
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < K; j++) {
            cin >> F[i][j];
        }
    }
 
    cin >> D;
    for(int i = 0; i < D; i++) {
        int A, B;
        cin >> A >> B; --A, --B;
        G[A].push_back(B);
        quant[B]++;
    }
 
    for(int i = 0; i < K; i++) {
        int val;
        cin >> val; order.push_back(val - 1);
    }
    while(order.size() < 4) order.push_back(0);
    mp[order] = 0; pat[0] = order;
    for(int i = 0; i < N; i++) {
        if(quant[i] == 0) {
            pq[0].push(Data(F[i][order[0]], F[i][order[1]], F[i][order[2]], F[i][order[3]], i));
        }
    }
    cin >> R;
    for(int i = 0; i < R; i++) {
        int m;
        cin >> m;
        process(m);
        for(int j = 0; j < K; j++) {
            cin >> order[j]; --order[j];
        }
        if(mp.count(order) == 0) {
            mp[order] = idx;
            pat[idx] = order;
            for(int j = 0; j < N; j++) {
                if(quant[j] == 0) {
                    pq[idx].push(Data(F[j][order[0]], F[j][order[1]], F[j][order[2]], F[j][order[3]], j));
                }
            }
            idx++;
        }
    }
    process(N);
 
    return (0);
}
 
void process(int m)
{
    int dat = mp[order];
    while(it < m) {
        while(!pq[dat].empty()) {
            Data d = pq[dat].top(); pq[dat].pop();
            if(flag[d.num]) continue;
            cout << d.num + 1 << endl;
            flag[d.num] = true;
            for(int i = 0; i < G[d.num].size(); i++) {
                int u = G[d.num][i];
                quant[u]--;
                if(quant[u] == 0) {
                    for(int j = 0; j < idx; j++) {
                        pq[j].push(Data(F[u][pat[j][0]], F[u][pat[j][1]], F[u][pat[j][2]], F[u][pat[j][3]], u));
                    }
                }
            }
            break;
        }
        it++;
    }
}
accuracy: 1
time_ms: 370
memory_kb: 29880
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, int> plp;
const ll INF = 1e9+100;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
const bool debug = 0;
//--------------------------------//

int N, K, D, R;
int f[50000][4];
vector<int> g[50000];
int cnt[50000];
int et[4], er[50001];
pll pri[50000][24];
map<int, int> ord;
bool done[50000];

inline int convert(int *a, int n) {
	int res = 0;
	for (int i = n - 1; i >= 0; i--) res = res * 10 + a[i];
	return res;
}

int main() {
	cin >> N >> K;
	int tmp[4] = {1,2,3,4};
	do {
		ord[convert(tmp, K)] = ord.size() - 1;
	} while (next_permutation(tmp, tmp + K));
	
	REP(i, N) REP(j, K) scanf("%d", &f[i][j]), f[i][j]--;
	cin >> D;
	REP(i, D) {
		int a, b;
		scanf("%d %d", &a, &b);
		g[a - 1].push_back(b - 1);
		cnt[b - 1]++;
	}
	REP(i, K) scanf("%d", et + i);
	cin >> R;
	
	memset(er, -1, sizeof(er));
	REP(i, R) {
		int m;
		scanf("%d", &m);
		REP(j, K) scanf("%d", tmp + j);
		er[m] = ord[convert(tmp, K)];
	}
	
	REP(i, N) {
		for (map<int, int>::iterator it = ord.begin(); it != ord.end(); ++it) {
			memset(tmp, 0, sizeof(tmp));
			
			tmp[0] = f[i][it->fi%10 - 1];
			if (K >= 2) tmp[1] = f[i][it->fi/10%10 - 1];
			if (K >= 3) tmp[2] = f[i][it->fi/100%10 - 1];
			if (K >= 4) tmp[3] = f[i][it->fi/1000 - 1];
			
			pri[i][it->se] = pll(tmp[0]*100000ll+tmp[1], tmp[2]*100000ll+tmp[3]);
		}
	}
	
	priority_queue<plp> pq[24];
	REP(i, N) if (cnt[i] == 0) {
		REP(j, 24) pq[j].push(plp(pri[i][j], i));
	}
	
	vector<int> ans;
	
	int e = ord[convert(et, K)];
	int fcnt = 0;
	while (!pq[e].empty()) {
		int pos = pq[e].top().se;
		pll tttt = pq[e].top().fi;
		
		pq[e].pop();
		if (done[pos]) continue;
		done[pos] = true;
		
		ans.push_back(pos + 1);
		
		REP(i, g[pos].size()) if (--cnt[g[pos][i]] == 0) {
			REP(j, 24) pq[j].push(plp(pri[g[pos][i]][j], g[pos][i]));
		}
		
		fcnt++;
		if (er[fcnt] != -1) e = er[fcnt];
	}
	
	REP(i, ans.size()) printf("%d\n", ans[i]);
	
	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 53600
final_rank: 7