Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> graph;
- vector<int> result, used;
- void pleaseKillMe(int v) {
- used[v] = 1;
- for (auto child : graph[v]) {
- if (used[child] == 0) {
- pleaseKillMe(child);
- }
- if (used[child] == 1) {
- cout << "No";
- exit(0);
- }
- }
- used[v] = 2;
- result.push_back(v);
- }
- int main() {
- size_t n, m;
- cin >> n >> m;
- graph.resize(n);
- used.assign(n, 0);
- for (size_t i = 0; i < m; i++) {
- size_t from, to;
- cin >> from >> to;
- graph[to - 1].push_back(from - 1);
- }
- for (size_t i = 0; i < n; i++) {
- if (used[i] == 0) {
- pleaseKillMe(i);
- }
- }
- cout << "Yes\n";
- for (auto soldier : result) {
- cout << soldier + 1 << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment