Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using pii=pair<int,int>;
- bool check=0;
- vector<int> H[100010];
- bool v[100010];
- void DFS(int i){
- v[i]=1;
- for(auto x:H[i]){
- if(v[x]){
- check=1;
- return;
- }else DFS(x);
- if(check==1) return;
- }
- v[i]=0;
- }
- int main(){
- int V,E,h,p;
- scanf("%d%d",&V,&E);
- vector<bool> visited(V+10,false);
- vector<int> hunted(V+10,0);
- queue<int> Q;
- priority_queue<pii,vector<pii>,greater<pii>> q;
- for(int i=0;i<E;i++){
- scanf("%d%d",&p,&h);
- H[h].push_back(p);
- hunted[p]++;
- }
- for(int i=1;i<=V;i++){
- check=0;
- DFS(i);
- if(check==1) break;
- }
- if(check==1){
- printf("No");
- return 0;
- }
- for(int i=1;i<=V;i++){
- if(hunted[i]==0){
- q.push({0,i});
- while(!q.empty()){
- int r=q.top().first;
- int c=q.top().second;
- Q.push(c);
- q.pop();
- visited[c]=true;
- for(auto x:H[c]){
- if(hunted[x]>1){
- hunted[x]--;
- continue;
- }else q.push({r+1,x});
- }
- }
- }
- }
- printf("Yes\n");
- while(!Q.empty()){
- printf("%d ",Q.front());
- Q.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment