我的程序 UB 了?
#include <bits/stdc++.h>
using namespace std;
int read(){
int f = 1, x = 0;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f=-1;
c=getchar();
}
while(isdigit(c)) {x=(x<<3)+(x<<1)+c-48, c=getchar();}
return x * f;
}
struct node{
int to, id;
bool operator < (const node &a) const {
return to < a.to;
}
};
int n, m, u, v, cnt = 1;
int in[100010], out[100010];
int stk[100010], top;
int s, t;
bool vis[200010];
vector<node> g[200010];
void addedge(int uu, int vv){
g[uu].push_back((node){vv, cnt++});
in[vv]++, out[uu]++;
// for(int i=1; i<=n; i++){
// printf("%3d %3d\n", in[i], out[i]);
// }
}
void dfs(int nw){
for(int i=0; i<g[nw].size(); i++){
if(vis[g[nw][i].id]) continue;
// cout<<nw<<"-"<<g[nw][i].to<<endl;
vis[g[nw][i].id] = true;
dfs(g[nw][i].to);
}
stk[++top] = nw;
}
bool judge(){
int c = 0;
for(int i=1; i<=n; i++){
if(in[i] != out[i]){
c++;
if(in[i] == out[i] - 1) s = i;
if(in[i] == out[i] + 1) t = i;
if(out[i]-in[i] > 1) return false;
}
}
if(c != 0 && c != 2) return false;
else if(c == 0) return s = t = 1, 1;
if(!s || !t) return false;
return true;
}
int main(){
n = read(), m = read();
for(int i=1; i<=m; i++){
u = read(), v = read();
// if(u == v) continue;
addedge(u, v);
}
if(!judge()){
puts("No");
return 0;
}
for(int i=1; i<=n; i++){
sort(g[i].begin(), g[i].end());
}
dfs(s);
while(top){
printf("%d ", stk[top--]);
}
return 0;
}