USACO Riding Fences 欧拉回路

参考: http://www.cppblog.com/Ylemzy/articles/100050.html

注意path数组长度1025edge个数开始501vertex个数结果test 8 

/*  
ID: wangxin12  
PROG: fence 
LANG: C++  
*/
#include <iostream>
#include <fstream>

#define MAX 505
#define Max(a,b) (a > b ? a : b)
#define Min(a,b) (a < b ? a : b)
using namespace std;



int map[MAX][MAX], num[MAX], path[1050]; //map¹¹Í¼£¬ num¼ÇÔØÿ¸öµã¶ÈÊý£¬path¼ÇÔØ·¾¶
int minn, maxn, index = 0; //ÒòΪÌâÄ¿ÖÐvertex²»´Ó1¿ªÊ¼£¬ÒªÔÚ¶ÁÈëÊý¾ÝµÄʱºò¼ÇÔØvertexµÄ×îС×î´óÖµ


void circle(int x) {
	int i;
	if(num[x]) {
		do {
			for(i = minn; i <= maxn; i++) {
				if(map[x][i]) 
					break;   
			}                              //find the smallest neighbor
			map[x][i]--, map[i][x]--;
			num[x]--, num[i]--;           //delete this edge
			circle(i);  //
		} while ( num[x] );
		path[index++] = x;
	}
	else 
		path[index++] = x;
}

void deal() {
	int i;
	for(i = minn; i <= maxn; i++) {
		if(num[i] % 2 == 1) {
			circle(i);
			return;
		}
	}
	circle(minn);
}


int main() {
	ifstream fi("fence.in");
	ofstream fo("fence.out");
	
	//intput
	int n, from, to, i;
	fi>>n;
	for(i = 1, minn = 500, maxn = 1; i <= n; i++) {
		fi>>from>>to;
		map[from][to]++, map[to][from]++;
		num[from]++, num[to]++;
		if( minn > Min(from, to) ) minn = Min(from, to);
		if( maxn < Max(from, to) ) maxn = Max(from, to);
	}	



	deal();

	
	//output
	for(int j = index - 1; j >= 0; j--) {
		fo<<path[j]<<endl;
	}

	fi.close();
	fo.close();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值