#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define x first
#define y second
#define endl '\n'
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int N = 2e5 + 10;
vector<int> a[N];
int n, m;
int ans[N];
int d[N];
vector<int> res;
bool topsort()
{
// d[i] 存储点i的入度
queue<int> q;
for (int i = 1; i <= n; i++) {
if (!d[i]) {
q.push(i);
}
}
while (q.size()) {
if(q.size() > 1) return 0; //如果此时的头有两个,就有两种走法,所以拓扑图一定不唯一
auto t = q.front();
res.push_back(t);
// ans[t] = f--;
q.pop();
for (auto x : a[t]) {
if (--d[x] == 0) {//这里保证搜到的点是一定把入度都变成0,即不可能有点再搜到它
q.push(x);
}
}
}
// 如果所有点都入队了,说明存在拓扑序列;否则不存在拓扑序列。
return res.size() == n;
/*为了理解,给出下面样例
1 3, 3 2,2 4,2 5,5 4不会出现在2的地方会搜到两条路,因为还有5-4这条边的存在。
*/
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
a[y].push_back(x);
d[x]++;
}
// cout << topsort() << endl;
if(topsort()){
cout << "Yes\n";
int t = n;
for(auto x : res)
ans[x] = t--;
// cout << x << " ";
for(int i = 1; i <= n; i++)
cout << ans[i] << " ";
cout << endl;
}
else cout << "No\n";
}
int main()
{
// IOS;
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
最后修改:2023 年 05 月 22 日
© 允许规范转载