結果

問題 No.918 LISGRID
ユーザー betrue12betrue12
提出日時 2019-10-26 14:33:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,555 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 175,580 KB
実行使用メモリ 13,544 KB
最終ジャッジ日時 2023-10-12 09:01:16
合計ジャッジ時間 5,864 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,128 KB
testcase_01 AC 22 ms
11,080 KB
testcase_02 AC 11 ms
8,444 KB
testcase_03 AC 12 ms
8,784 KB
testcase_04 AC 11 ms
8,196 KB
testcase_05 AC 28 ms
11,648 KB
testcase_06 AC 27 ms
11,468 KB
testcase_07 AC 37 ms
13,284 KB
testcase_08 AC 22 ms
10,644 KB
testcase_09 AC 31 ms
12,060 KB
testcase_10 AC 29 ms
11,700 KB
testcase_11 AC 31 ms
11,700 KB
testcase_12 AC 36 ms
12,296 KB
testcase_13 AC 29 ms
11,176 KB
testcase_14 AC 38 ms
12,956 KB
testcase_15 AC 42 ms
13,544 KB
testcase_16 AC 17 ms
9,300 KB
testcase_17 AC 18 ms
9,692 KB
testcase_18 AC 17 ms
9,260 KB
testcase_19 AC 8 ms
7,912 KB
testcase_20 AC 23 ms
10,328 KB
testcase_21 AC 9 ms
8,136 KB
testcase_22 AC 7 ms
7,768 KB
testcase_23 AC 16 ms
9,056 KB
testcase_24 AC 4 ms
7,232 KB
testcase_25 AC 5 ms
7,128 KB
testcase_26 AC 4 ms
7,096 KB
testcase_27 AC 3 ms
7,096 KB
testcase_28 AC 3 ms
7,156 KB
testcase_29 AC 4 ms
7,164 KB
testcase_30 AC 4 ms
7,092 KB
testcase_31 AC 3 ms
7,188 KB
testcase_32 AC 4 ms
7,160 KB
testcase_33 AC 5 ms
7,108 KB
testcase_34 AC 5 ms
7,260 KB
testcase_35 AC 5 ms
7,428 KB
testcase_36 AC 4 ms
7,172 KB
testcase_37 AC 22 ms
10,484 KB
testcase_38 AC 16 ms
9,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct TopologicalSort{
    vector<int> rank;
    vector<int> ordered_indices;
    bool valid;

    TopologicalSort(int N, vector<int> G[]){
        rank.resize(N, -1);
        ordered_indices.resize(N, -1);

        vector<int> indeg(N, 0);
        for(int i=0; i<N; i++) for(int j : G[i]) indeg[j]++;
        stack<int> st;
        for(int i=0; i<N; i++) if(indeg[i] == 0) st.push(i);

        int num = 0;
        while(st.size()){
            int i = st.top(); st.pop();
            rank[i] = num;
            ordered_indices[num] = i;
            num++;
            for(int j : G[i]){
                indeg[j]--;
                if(indeg[j] == 0) st.push(j);
            }
        }
        // 閉路があるとfalse
        valid = (num == N);
    }
};

int main(){
    int H, W, A[400], B[400];
    cin >> H >> W;
    for(int i=0; i<H; i++) cin >> A[i];
    for(int i=0; i<W; i++) cin >> B[i];
    sort(A, A+H);
    sort(B, B+W);

    vector<int> edges[400*400];
    for(int i=0; i<H; i++) for(int j=0; j<W; j++){
        if(i){
            int from = (i-1)*W + j, to = i*W + j;
            if(B[j] <= i) swap(from, to);
            edges[from].push_back(to);
        }
        if(j){
            int from = i*W + j - 1, to = i*W + j;
            if(A[i] <= j) swap(from, to);
            edges[from].push_back(to);
        }
    }

    TopologicalSort ts(H*W, edges);
    assert(ts.valid);
    for(int i=0; i<H; i++) for(int j=0; j<W; j++) cout << ts.rank[i*W+j]+1 << " \n"[j==W-1];
    return 0;
}
0