結果

問題 No.1477 Lamps on Graph
ユーザー Example0911Example0911
提出日時 2021-04-16 20:33:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,461 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 209,036 KB
実行使用メモリ 10,656 KB
最終ジャッジ日時 2023-09-15 21:27:58
合計ジャッジ時間 7,564 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 63 ms
6,152 KB
testcase_13 AC 68 ms
5,948 KB
testcase_14 AC 79 ms
6,504 KB
testcase_15 AC 28 ms
4,648 KB
testcase_16 AC 22 ms
5,080 KB
testcase_17 AC 26 ms
4,772 KB
testcase_18 AC 115 ms
7,708 KB
testcase_19 AC 68 ms
6,584 KB
testcase_20 AC 21 ms
4,380 KB
testcase_21 AC 103 ms
6,788 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 38 ms
4,760 KB
testcase_24 AC 106 ms
7,168 KB
testcase_25 AC 28 ms
4,380 KB
testcase_26 AC 102 ms
7,672 KB
testcase_27 AC 32 ms
4,896 KB
testcase_28 AC 44 ms
5,840 KB
testcase_29 AC 47 ms
5,092 KB
testcase_30 AC 72 ms
5,500 KB
testcase_31 AC 39 ms
4,984 KB
testcase_32 AC 179 ms
10,656 KB
testcase_33 AC 132 ms
9,356 KB
testcase_34 AC 171 ms
7,592 KB
testcase_35 AC 136 ms
9,004 KB
testcase_36 AC 132 ms
9,148 KB
testcase_37 AC 92 ms
9,024 KB
testcase_38 AC 108 ms
9,016 KB
testcase_39 AC 128 ms
9,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
//#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N, M; cin >> N >> M;
	vector<int>A(N); for (int i = 0; i < N; i++)cin >> A[i];
	vector<vector<int>>G(N);
	for (int i = 0; i < M; i++) {
		int u, v; cin >> u >> v; u--; v--;
		if (A[u] < A[v]) {
			G[u].push_back(v);
		}
		else if (A[u] > A[v]) {
			G[v].push_back(u);
		}
	}
	int K; cin >> K;
	vector<bool>now(N, false);
	for (int i = 0; i < K; i++) {
		int B; cin >> B; B--;
		now[B] = true;
	}
	vector<int>ans;
	vector<int>ind(N);
	for (int i = 0; i < N; i++) {
		for (auto x : G[i]) {
			ind[x]++;
		}
	}
	queue<int> que;
	for (int i = 0; i < N; i++) {  // 次数が0の点をキューに入れる
		if (ind[i] == 0) {
			que.push(i);
		}
	}
	while (!que.empty()) {  // 幅優先探索
		int t = que.front();
		ans.push_back(t);
		que.pop();
		for (auto e : G[t]) {
			ind[e]--;
			if (ind[e] == 0) {
				que.push(e);
			}
		}
	}
	if (ans.size() != N) {
		cout << -1 << endl; return 0;
	}
	int cnt = 0;
	vector<int>res;
	for (int i = 0; i < N; i++) {
		if (now[ans[i]]) {
			cnt++;
			res.push_back(ans[i] + 1);
			now[ans[i]] = false;
			for (auto v : G[ans[i]]) {
				if (now[v])now[v] = false;
				else now[v] = true;
			}
		}
	}
	cout << cnt << endl;
	for (int i = 0; i < cnt; i++) {
		cout << res[i] << endl;
	}
	return 0;
}
0