#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+10;
vector<ll> G[N],ans;
ll n,m,du[N],cur[N],s=1;
void dfs(ll u){
for(ll i=cur[u];i<G[u].size();i=cur[u]){
cur[u]=i+1;
dfs(G[u][i]);
}
ans.push_back(u);
}
int main(){
cin >> n >> m;
for(ll i=1;i<=m;i++){
ll u,v;
cin >> u >> v;
G[u].push_back(v);
du[u]++,du[v]--;
}
for(ll i=1;i<=n;i++)sort(G[i].begin(),G[i].end());
ll cnt1=0,cnt2=0,cnt3=0;
for(ll i=1;i<=n;i++){
if(du[i]==0)cnt1++;
else if(du[i]==1)cnt2++,s=i;
else if(du[i]==-1)cnt3++;
}
if(cnt2>1||cnt3>1||cnt1+cnt2+cnt3!=n){
cout << "No";
return 0;
}
dfs(s);
reverse(ans.begin(),ans.end());
for(auto i:ans)cout << i << ' ';
return 0;
}
标签:cur,ll,路径,long,cnt3,du,欧拉
From: https://www.cnblogs.com/ningziang/p/17985009