44pts 求调

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

sfb1363II @ 2024-10-18 23:50:04

记录

#include<bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
set<int> st;
int m,x,cnt;
int path[5500];
bool f,vis[5500];
vector<pii> edge;
vector<int> g[5500];
void dfs(int u){
    for(int i=0;i<g[u].size();i++){
        int v;
        pii x=edge[g[u][i]];
        if(x.first!=u) v=x.first;
        else v=x.second;
        if(!vis[g[u][i]]){
            vis[g[u][i]]=1; 
            dfs(v);
            path[++cnt]=v;
        }
    }
}
int main(){
    cin>>m;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        edge.push_back({u,v});
        g[u].push_back(x);
        g[v].push_back(x);
        x++;
        st.insert(u);
        st.insert(v);
    }
    for(auto i:st){
        if(g[i].size()%2){
            f=true;
            dfs(i);
            path[++cnt]=i;
            break;
        }
    }
    if(!f){
        dfs(1);
        path[++cnt]=1;
    }
    for(int i=cnt;i>=1;i--)
        cout<<path[i]<<"\n";
    return 0;
}

|