結果

問題 No.918 LISGRID
ユーザー betrue12
提出日時 2019-10-26 14:33:40
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,555 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 176,520 KB
実行使用メモリ 13,792 KB
最終ジャッジ日時 2024-09-14 07:59:29
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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