Skip to code
Skip to analysis

This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!

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.

PROGRAM NAME: fence

INPUT FORMAT

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.

SAMPLE INPUT (file fence.in)

9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 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 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.

SAMPLE OUTPUT (file fence.out)

1
2
3
4
2
5
4
6
5
7

CODE

Java


C++


Pascal



ANALYSIS

Hal Burch

Assuming you pick the lowest index vertex connected to each node, the Eulerian Path algorithm actually determines the path requested, although in the reverse direction. You must start the path determination at the lowest legal vertex for this to work.

/* Prob #5: Riding the Fences */
#include <stdio.h>
#include <string.h>

#define MAXI 500
#define MAXF 1200
char conn[MAXI][MAXI];
int deg[MAXI];
int nconn;

int touched[MAXI];

int path[MAXF];
int plen;

/* Sanity check routine */
void fill(int loc)
 {
  int lv;

  touched[loc] = 1;
  for (lv = 0; lv < nconn; lv++)
    if (conn[loc][lv] && !touched[lv])
      fill(lv);
 }

/* Sanity check routine */
int is_connected(int st)
 {
  int lv;
  memset(touched, 0, sizeof(touched));
  fill(st);
  for (lv = 0; lv < nconn; lv++)
    if (deg[lv] && !touched[lv])
      return 0;
  return 1;
 }

/* this is exactly the Eulerian Path algorithm */
void find_path(int loc)
 {
  int lv;

  for (lv = 0; lv < nconn; lv++)
    if (conn[loc][lv])
     {
      /* delete edge */
      conn[loc][lv]--;
      conn[lv][loc]--;
      deg[lv]--;
      deg[loc]--;

      /* find path from new location */
      find_path(lv);
     }

  /* add this node to the `end' of the path */
  path[plen++] = loc;
 }

int main(int argc, char **argv)
 {
  FILE *fin, *fout;
  int nfen;
  int lv;
  int x, y;

  if (argc == 1) 
   {
    if ((fin = fopen("fence.in", "r")) == NULL)
     {
      perror ("fopen fin");
      exit(1);
     }
    if ((fout = fopen("fence.out", "w")) == NULL)
     {
      perror ("fopen fout");
      exit(1);
     }
   } else {
    if ((fin = fopen(argv[1], "r")) == NULL)
     {
      perror ("fopen fin filename");
      exit(1);
     }
    fout = stdout;
   }

  fscanf (fin, "%d", &nfen);
  for (lv = 0; lv < nfen; lv++)
   {
    fscanf (fin, "%d %d", &x, &y);
    x--; y--;
    conn[x][y]++;
    conn[y][x]++;
    deg[x]++;
    deg[y]++;
    if (x >= nconn) nconn = x+1;
    if (y >= nconn) nconn = y+1;
   }

  /* find first node of odd degree */
  for (lv = 0; lv < nconn; lv++)
    if (deg[lv] % 2 == 1) break;
  /* if no odd-degree node, find first node with non-zero degree */
  if (lv >= nconn)
    for (lv = 0; lv < nconn; lv++)
      if (deg[lv]) break;
#ifdef CHECKSANE
  if (!is_connected(lv)) /* input sanity check */
   {
    fprintf (stderr, "Not connected?!?\n");
    return 0;
   }
#endif

  /* find the eulerian path */
  find_path(lv); 

  /* the path is discovered in reverse order */
  for (lv = plen-1; lv >= 0; lv--)
    fprintf (fout, "%i\n", path[lv]+1);
  return 0;
 }

Back to top