Submission #576255

# Submission time Handle Problem Language Result Execution time Memory
576255 2022-06-12T18:57:36 Z classic Alkemija (COCI18_alkemija) C++14
80 / 80
50 ms 11332 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> a(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i];
		a[i]--;
	}
	int k;
	cin >> k;
	vector<vector<int>> adj1(n);
	vector<vector<int>> adj2(n);
	vector<int> deg(n);
	for (int i = 0; i < k; i++) {
		int l, r;
		cin >> l >> r;
		deg[i] = l;
		while (l--) {
			int x;
			cin >> x;
			x--;
			adj1[x].emplace_back(i);
		}
		while (r--) {
			int x;
			cin >> x;
			x--;
			adj2[i].emplace_back(x);
		}
	}
	vector<bool> has(n);
	queue<int> que;
	for (int i = 0; i < m; i++) {
		if (!has[a[i]]) {
			has[a[i]] = 1;
			for (int x : adj1[a[i]]) {
				deg[x] -= 1;
				if (deg[x] == 0) {
					que.emplace(x);
				}
			}
		}
	}
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		for (int v : adj2[u]) {
			if (!has[v]) {
				has[v] = 1;
				for (int x : adj1[v]) {
					deg[x] -= 1;
					if (deg[x] == 0) {
						que.emplace(x);
					}
				}
			}
		}
	}
	vector<int> res;
	for (int i = 0; i < n; i++) {
		if (has[i]) {
			res.emplace_back(i);
		}
	}
	cout << (int)res.size() << '\n';
	for (int x : res) {
		cout << x + 1 << ' ';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 3228 KB Output is correct
2 Correct 18 ms 4052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 6092 KB Output is correct
2 Correct 36 ms 7116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 8860 KB Output is correct
2 Correct 37 ms 8288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 11332 KB Output is correct
2 Correct 44 ms 10580 KB Output is correct