luogu P2731 骑马修栅栏 Riding the Fences

欧拉回路。

入度为奇数的点,搜他。

最好邻接矩阵。。。                                                                                                                                                                                                                                                                                                                                                 

#include<cstdio>
#include<iostream>
#define R register int
using namespace std;
inline int g() {
    R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
    do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
}
int m,top,mn,mx,st=1;
int e[501][501],stk[1025],r[501];
void dfs(int u) {
    for(R v=mn;v<=mx;++v) if(e[u][v])
        --e[u][v],--e[v][u],dfs(v);
    stk[++top]=u;
}
signed main() {
    m=g();
    for(R i=1,u,v;i<=m;++i) u=g(),v=g(),
        ++e[u][v],++e[v][u],++r[u],++r[v],
        mn=min(min(u,v),mn),mx=max(max(u,v),mx);
    for(R i=mn;i<=mx;++i) if(r[i]&1) {st=i; break;}
    dfs(st); for(;top>0;--top) printf("%d\n",stk[top]);
}

2019.04.11

posted @ 2019-04-11 18:49  LuitaryiJack  阅读(155)  评论(0编辑  收藏  举报