結果

問題 No.1477 Lamps on Graph
コンテスト
ユーザー y61mpnl
提出日時 2021-04-16 21:47:18
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,038 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,305 ms
コンパイル使用メモリ 218,072 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2026-06-19 19:21:52
合計ジャッジ時間 4,481 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#define REP(i,b,e) for(int i=(b);i<(e);i++)

int main(){
	int n, m;
	scanf("%d %d", &n, &m);

	int a[n];
	REP(i, 0, n) scanf("%d", &a[i]);

	int in[n] = {};
	std::vector<int> links[n];
	REP(i, 0, m){
		int u, v;
		scanf("%d %d", &u, &v);
		u--, v--;
		if(a[u]<a[v]){
			links[u].push_back(v);
			in[v]++;
		}else{
			links[v].push_back(u);
			in[u]++;
		}
	}

	bool on[n] = {};
	int k;
	scanf("%d", &k);
	REP(i, 0, k){
		int b;
		scanf("%d", &b);
		on[b-1] = true;
	}

	std::queue<int> que;
	REP(i, 0, n) if(in[i]==0) que.push(i);

	std::vector<int> ans;
	while(!que.empty()){
		int from = que.front();
		que.pop();

		if(!on[from]){
			for(int to: links[from]){
				if(--in[to]==0) que.push(to);
			}
		}else{
			on[from] ^= 1;
			ans.push_back(from+1);
			for(int to: links[from]){
				on[to] ^= 1;
				if(--in[to]==0) que.push(to);
			}
		}
	}

	bool ok = true;
	REP(i, 0, n) if(on[i]) ok = false;
	if(!ok) puts("-1");
	else{
		printf("%ld\n", ans.size());
		for(int x: ans) printf("%d\n", x);
	}

	return 0;
}
0