Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<vector>
- #include<stack>
- using namespace std;
- int n,m;
- vector<int> adj[100010];
- int color[100010];
- bool iscycle = false;
- stack<int> ans;
- void DFS(int u){
- if(color[u] == 2){
- return;
- }
- if(color[u] == 1){
- iscycle = true;
- return;
- }
- color[u] = 1;
- for(int i=0;i<adj[u].size();i++){
- int v = adj[u][i];
- DFS(v);
- }
- color[u] = 2;
- ans.push(u);
- }
- int main()
- {
- scanf("%d %d",&n,&m);
- for(int i=0;i<m;i++){
- int u,v;
- scanf("%d %d",&v,&u);
- adj[u].push_back(v);
- }
- for(int i=1;i<=n;i++){
- if(color[i] == 0){
- DFS(i);
- }
- }
- if(!iscycle){
- printf("Yes\n");
- while(!ans.empty()){
- printf("%d ",ans.top());
- ans.pop();
- }
- }
- else{
- printf("No");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment