結果

問題 No.1477 Lamps on Graph
ユーザー Mister
提出日時 2021-04-16 21:05:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 1,129 bytes
コンパイル時間 1,428 ms
コンパイル使用メモリ 85,468 KB
最終ジャッジ日時 2025-01-20 19:14:39
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> xs(n);
    for (auto& x : xs) cin >> x;

    vector<vector<int>> graph(n);
    while (m--) {
        int u, v;
        cin >> u >> v;
        --u, --v;

        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    vector<bool> stat(n, false);
    {
        int k;
        cin >> k;
        while (k--) {
            int v;
            cin >> v;
            stat[--v] = true;
        }
    }

    vector<int> vs(n);
    iota(vs.begin(), vs.end(), 0);
    sort(vs.begin(), vs.end(),
         [&](int u, int v) { return xs[u] < xs[v]; });

    vector<int> ans;
    for (auto v : vs) {
        if (!stat[v]) continue;
        ans.push_back(v + 1);

        stat[v] = !stat[v];
        for (auto u : graph[v]) {
            if (xs[u] > xs[v]) stat[u] = !stat[u];
        }
    }

    cout << ans.size() << "\n";
    for (auto v : ans) cout << v << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0