結果

問題 No.1477 Lamps on Graph
ユーザー rogi52rogi52
提出日時 2022-10-09 23:42:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,542 bytes
コンパイル時間 2,333 ms
コンパイル使用メモリ 207,364 KB
実行使用メモリ 10,788 KB
最終ジャッジ日時 2023-09-06 02:10:24
合計ジャッジ時間 15,125 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 RE -
testcase_04 AC 2 ms
4,380 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,388 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 RE -
testcase_11 AC 2 ms
4,384 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 WA -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 53 ms
10,788 KB
testcase_33 AC 53 ms
9,680 KB
testcase_34 AC 46 ms
7,764 KB
testcase_35 WA -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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(K);
    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