Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- using namespace std;
- using ll = long long;
- using ld = long double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- int n,m;
- vector <vector<int>>G;
- const int wh = 0, gr = 1, bl = 2;
- vector <int> clr;
- vector <int> tp;
- //int cnt = 0;//connected
- void dfs(int v, int pr){
- clr[v] = gr;
- for (int u: G[v]){
- if (u == pr) continue;
- else if (clr[u] == wh){
- dfs(u, v);
- for (int x: G[v]){
- if (clr[x] != bl){
- cout<<"NO\n";
- exit(0);
- }
- }
- }
- else if (clr[u] == gr){
- cout<<"NO\n";
- exit(0);
- }
- }
- clr[v] = bl;
- tp.push_back(v+1);
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n>>m;
- G.resize(n);
- clr.assign(n, wh);
- vector <int> in(n,0), out(n, 0);
- bool can = 1;
- for (int i=0;i<m;++i){
- int a,b; cin>>a>>b;
- a--; b--;
- G[a].push_back(b);
- in[b]++;
- out[a]++;
- }
- if (count(in.begin(), in.end(), 0) != 1){
- can = 0;
- }
- if (count(out.begin(), out.end(), 0) != 1){
- can = 0;
- }
- if (!can){
- cout<<"NO\n";
- exit(0);
- }
- for (int i = 0;i<n;++i){
- if (clr[i] == wh){
- dfs(i, -1);
- }
- }
- cout<<"YES\n";
- reverse(tp.begin(), tp.end());
- cv(tp);
- }
- /*
- КОНТРПРИМЕР:
- 3 3
- 1 2
- 2 3
- 1 3
- YES
- 1 2 3
- */
Add Comment
Please, Sign In to add comment