結果

問題 No.1477 Lamps on Graph
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-04-22 09:22:29
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 123,064 KB
実行使用メモリ 13,748 KB
最終ジャッジ日時 2024-11-06 23:29:42
合計ジャッジ時間 9,907 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    long long N, M, A, B, C, start, K;
    long long from, alt, d;
    cin >> N >> M;
    pq<pair<long long, long long>> que;
    vector<long long> num(N), ans;
    deque<bool> color(N);
    for (int i=0; i<N; i++) cin >> num[i];
    vector<vector<long long>> E(N);
 
    for (int i=0; i < M; i++){
        cin >> A >> B;
        A--; B--;
        E[A].push_back(B);
        E[B].push_back(A);
    }

    cin >> K;
    for (int i=0; i<K; i++){
        cin >> B; B--;
        color[B] = 1;
        que.push({num[B], B});
    }
 
    while(!que.empty()){
        tie(d, from) = que.top();
        que.pop();
        if (color[from] == 0) continue;
        color[from] = 0;
        ans.push_back(from+1);
        for (auto to : E[from]){
            if (num[to] > num[from]){
                color[to] ^= 1;
                if (color[to] == 1){
                    que.push({num[to], to});
                }
            }
        }
    }

    cout << ans.size() << endl;
    for (auto x : ans) cout << x << endl;
 
    return 0;
}
0