Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstdio>
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- bool dfs(vector<vector<int>>& lin, vector<int>& answer, vector<bool>& use, int& c, int& n)
- {
- if (use[c] && answer[c] == 0)
- return false;
- use[c] = true;
- bool sortable = true;
- for (int i = 1; i < lin[c].size(); i++)
- {
- //if (use[i] && answer[i] == 0)
- //return false;
- if (!use[i])
- sortable = sortable && dfs(lin, answer, use, lin[c][i], n);
- }
- answer[n] = c;
- n -= 1;
- return sortable;
- }
- int main()
- {
- int n, m, a, b, d;
- scanf("%d %d", &n, &m);
- d = n;
- vector<vector<int>> line(n + 1, vector<int>(1));
- for (int i = 1; i < m; i++)
- {
- scanf("%d %d", &a, &b);
- line[a].push_back(b);
- }
- vector<int> ans(n + 1, 0);
- vector<bool> used(n + 1, false);
- for (int i = 1; i <= d; i++)
- if (!used[i])
- if (!dfs(line, ans, used, i, n))
- {
- printf("No");
- return 0;
- }
- printf("Yes\n");
- for (int i = 1; i <= d; i++)
- printf("%d ", ans[i]);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement