Usaco 3.3 Riding The Fences

Source: Internet
Author: User

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 the them (and nowhere else) and fixing the broken parts.

Farmer John is as a 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 E Xactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

Every fence connects, fence intersections, which is numbered inclusively from 1 through (though some farms has FA R fewer than 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 is "connected").

Your program must output the path of intersections that, if interpreted as a base number, would has the smallest magn Itude.

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

Program Name:fenceinput FORMAT
Line 1: The number of fences, F (1 <= F <= 1024)
Line 2..f+1: A pair of integers (1 <= i,j <=) that's tell which pair of intersections this fence connects.
SAMPLE INPUT (file fence.in)
91 22 33 44 24 52 55 65 74 6
OUTPUT FORMAT

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 O N, 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.

SAMPLE OUTPUT (file fence.out)
1234254657

——————————————————————————

Ask for a Euler path or a Euler circuit

This is the 500 binary format problem ah ... It's always going to start at the smallest point, so we can use the adjacency matrix to store

If there is an odd point, then the starting point must be this, and the end point will not come back.

If all the points are even, then the smallest point is the starting point and the end will come back here.

1 /*2 Id:ivorysi3 prog:fence4 lang:c++5 */6#include <iostream>7#include <cstdio>8#include <cstring>9#include <algorithm>Ten#include <queue> One#include <Set> A#include <vector> - #defineSiji (i,x,y) for (int i= (x); i<= (y); ++i) - #defineGongzi (j,x,y) for (int j= (x); j>= (y);--j) the #defineXiaosiji (i,x,y) for (int i= (x);i< (y); ++i) - #defineSigongzi (j,x,y) for (int j= (x);j> (y);--j) - #defineINF 0x7fffffff - #defineMAXN 400005 + #defineIvorysi - #defineMo 97797977 + #defineHa 974711 A #defineBA 47 at #defineFi first - #defineSe Second - #definePII pair<int,int> - using namespacestd; -typedefLong Longll; - intadj[ -][ -]; in intsize[ -]; - intpo[605]; to intpath[ .],cnt; + voidinit () { -     intf,u,v; thescanf"%d",&f); *Siji (I,1, F) { $scanf"%d%d",&u,&v);Panax Notoginseng++size[u];++Size[v]; -++adj[u][v];++Adj[v][u]; the     } + } A voidDfsintu) { the      while(po[u]<= -) { +          while(Adj[u][po[u]]) { ---adj[po[u]][u];--Adj[u][po[u]]; $ DFS (Po[u]); $         } -++po[u];//add it to the bottom . -     } thepath[++cnt]=u; - }Wuyi voidsolve () { the init (); -     intval=-1; WuSiji (I,1, -)if(size[i]%2) {val=i; Break;} -     if(val==-1) { AboutSiji (I,1, -)if(size[i]!=0) {val=i; Break;} $     } - Dfs (val); -Gongzi (i,cnt,1) { -printf"%d\n", Path[i]); A     } + } the intMainintargcChar Const*argv[]) - { $ #ifdef Ivorysi theFreopen ("fence.in","R", stdin); theFreopen ("Fence.out","W", stdout); the #else theFreopen ("f1.in","R", stdin); - #endif in solve (); the}

Http://www.cnblogs.com/ivorysi/p/5745005.html

Previously written by Super Earnest Euler circuit ... But in fact, Usaco is very good at speaking.

Usaco 3.3 Riding The Fences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.