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;
- vector<vector<int>> lin;
- int cur_max_number;
- vector<int> ans, use1, use2;
- bool dfs(int c)
- {
- if (use1[c] && ans[c] == 0)
- return false;
- use1[c] = true;
- bool sortable = true;
- for (int i = 1; i < lin[c].size(); i++)
- {
- if (!use1[lin[c][i]])
- sortable = sortable && dfs(lin[c][i]);
- else if (!use2[lin[c][i]])
- {
- return false;
- }
- }
- use2[c] = true;
- ans[cur_max_number] = c;
- cur_max_number -= 1;
- return sortable;
- }
- int main()
- {
- int n, m, a, b, d;
- scanf("%d %d", &n, &m);
- cur_max_number = n;
- lin.resize(n + 1, vector<int>(1));
- for (int i = 0; i < m; i++)
- {
- scanf("%d %d", &a, &b);
- lin[a].push_back(b);
- }
- ans.resize(n + 1, 0);
- use1.resize(n + 1, false);
- use2.resize(n + 1, false);
- for (int i = 1; i <= n; i++)
- if (!use1[i])
- if (!dfs(i))
- {
- printf("No");
- return 0;
- }
- printf("Yes\n");
- for (int i = 1; i <= n; i++)
- printf("%d ", ans[i]);
- return 0;
- }
Add Comment
Please, Sign In to add comment