24分样例没过求调

P2731 [USACO3.3] 骑马修栅栏 Riding the Fences

Cosine_Func @ 2024-02-24 16:10:59

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ve vector
#define endl '\n'
#define itn int
#define AC return 0
#define len length
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const itn N=5e2+5;
ve<int> edge[N];
int indg[N],outdg[N],m,n,s,t;
bool vis[N];
void add(int u,int v){
    edge[u].pb(v);
    indg[v]++;
    outdg[u]++;
}
stack<int> ans;
void dfs(int u){
    for(int i=0;i<edge[u].size();i++){
        if(edge[u][i]!=-1){
            int v=edge[u][i];
            edge[u][i]=-1;
            dfs(v);
        }
    }
    ans.push(u);
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    //begin
    cin>>m;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        n=max(n,max(u,v));
        add(u,v);
    }
    for(itn i=1;i<=n;i++){
        if(indg[i]-outdg[i]==1){
            if(t){
                cout<<"No";
                AC;
            }
            t=i;
        }
        if(outdg[i]-indg[i]==1){
            if(s){
                cout<<"No";
                AC;
            }
            s=i;
        }
    }
    for(int i=1;i<=n;i++)
        sort(edge[i].begin(),edge[i].end());
    s=s?s:1;
    dfs(s);
    while(!ans.empty()){
        cout<<ans.top()<<endl;
        ans.pop();
    }
    //end
    AC;
}

|