Riding the Fences(欧拉回路)

描述

Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.

Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are “connected”).

Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

There will always be at least one solution for each set of input data supplied to your program for testing.

输入

Line 1: The number of fences, F (1 <= F <= 1024)

Line 2…F+1: A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

输出

The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection’s number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

样例输入

9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6

样例输出

1
2
3
4
2
5
4
6
5
7
这题主要是要注意输入可能会有重边,会有很多的边,所以存边的数组要开的稍微大一点,主要是经验不足吧

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505;
int degree[N],G[N][N];
int k,ans[100005];
void f(int u){
    int v;
    for(v=1;v<=500;v++){
        if(G[u][v]){
            G[u][v]--;
            G[v][u]--;
            f(v);
            ans[k++]=v;
        }
    }
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        k=0;
        memset(degree,0,sizeof(degree));
        memset(G,0,sizeof(G));
        int x,y,ma=0,flag=1;
        for(int i=0;i<n;i++){
            scanf("%d%d",&x,&y);
            degree[x]++;
            degree[y]++;
            G[x][y]++;
            G[y][x]++;
            ma=max(max(ma,x),y);
        }
        for(int i=1;i<=ma;i++){
            if(degree[i]%2){
                f(i);
                flag=0;
                printf("%d\n",i);
                break;
            }
        }
        if(flag){
            for(int i=1;i<=ma;i++){
                if(degree[i]!=0){
                    f(i);
                    printf("%d\n",i);
                    break;
                }
            }
        }
        for(int i=k-1;i>=0;i--){
            printf("%d\n",ans[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值