結果

問題 No.1477 Lamps on Graph
ユーザー rogi52rogi52
提出日時 2022-10-09 23:47:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,547 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 207,244 KB
実行使用メモリ 10,736 KB
最終ジャッジ日時 2023-09-06 02:14:44
合計ジャッジ時間 6,426 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 36 ms
6,220 KB
testcase_13 AC 34 ms
6,140 KB
testcase_14 AC 44 ms
6,580 KB
testcase_15 AC 20 ms
4,712 KB
testcase_16 AC 13 ms
5,056 KB
testcase_17 AC 12 ms
5,084 KB
testcase_18 AC 32 ms
7,660 KB
testcase_19 AC 29 ms
6,396 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 28 ms
6,956 KB
testcase_22 AC 9 ms
4,384 KB
testcase_23 AC 26 ms
4,872 KB
testcase_24 AC 46 ms
7,304 KB
testcase_25 AC 17 ms
4,428 KB
testcase_26 AC 41 ms
7,924 KB
testcase_27 AC 24 ms
5,048 KB
testcase_28 AC 26 ms
5,992 KB
testcase_29 AC 26 ms
5,176 KB
testcase_30 AC 16 ms
5,436 KB
testcase_31 AC 14 ms
5,244 KB
testcase_32 AC 52 ms
10,736 KB
testcase_33 AC 48 ms
9,712 KB
testcase_34 AC 45 ms
7,716 KB
testcase_35 AC 56 ms
9,348 KB
testcase_36 AC 57 ms
9,236 KB
testcase_37 AC 50 ms
9,416 KB
testcase_38 AC 52 ms
9,312 KB
testcase_39 AC 57 ms
9,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

template< typename T >
vector<int> topological_sort(const vector<vector<T>> &G) {
    const int N = (int)G.size();
    vector<int> deg(N,0);
    for(int i = 0; i < N; i++){
        for(auto &to : G[i]) ++deg[to];
    }
    queue<int> q;
    for(int i = 0; i < N; i++){
        if(deg[i] == 0) q.push(i);
    }
    vector<int> ord;
    while(!q.empty()){
        int p = q.front(); q.pop();
        ord.push_back(p);
        for(auto &to : G[p]){
            if(--deg[to] == 0) q.push(to);
        }
    }
    return ord; // ord.size() == N => DAG
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    vector<int> A(N);
    rep(i,N) cin >> A[i];
    vector<vector<int>> G(N);
    rep(i,M) {
        int u,v; cin >> u >> v; u--; v--;
        if(A[u] < A[v]) G[u].push_back(v);
        if(A[u] > A[v]) G[v].push_back(u);
    }

    int K; cin >> K;
    vector<int> C(N, 0);
    rep(i,K) {
        int b; cin >> b; b--;
        C[b] = 1;
    }

    vector<int> ans;
    vector<int> ord = topological_sort(G);
    for(int v : ord) {
        if(C[v] == 1) {
            C[v] = 0;
            ans.push_back(v);
            for(int to : G[v]) {
                C[to] ^= 1;
            }
        }
    }

    int ok = 1;
    rep(i,N) ok &= (C[i] == 0);
    if(ok) {
        cout << ans.size() << endl;
        for(int a : ans) cout << a + 1 << "\n";
    } else {
        cout << -1 << endl;
    }
}
0