USACO 3.3 Riding The Fences

TASK: fence
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 9632 KB]
   Test 2: TEST OK [0.000 secs, 9632 KB]
   Test 3: TEST OK [0.000 secs, 9632 KB]
   Test 4: TEST OK [0.000 secs, 9632 KB]
   Test 5: TEST OK [0.000 secs, 9632 KB]
   Test 6: TEST OK [0.000 secs, 9632 KB]
   Test 7: TEST OK [0.000 secs, 9632 KB]
   Test 8: TEST OK [0.000 secs, 9632 KB]

All tests OK.

1 /*
2 PROG: fence
3 ID: jiafeim1
4 LANG: C++
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9  #define MAXN 1300
10 #define maxX(x,y) ((x)>(y)?(x):(y))
11 int mats[MAXN][MAXN]={0};
12 int path[MAXN];
13 int degree[MAXN]={0};
14 void find_path_u(int n,int mat[][MAXN],int now,int& step,int* path)
15 {
16 int i;
17 for (i=0;i<n;++i)
18 {
19 while (mat[now][i])
20 {
21 mat[now][i]--,mat[i][now]--;
22 find_path_u(n,mat,i,step,path);
23 }
24 }
25 path[step++]=now;
26 }
27
28 int main()
29 {
30 FILE *fin = fopen("fence.in", "r");
31 FILE *fout = fopen("fence.out", "w");
32
33 int road_num;
34 fscanf(fin,"%d",&road_num);
35 int t1,t2;
36 int maxNum=-1;
37 for(int i = 0;i<road_num;++i)
38 {
39 fscanf(fin,"%d%d",&t1,&t2);
40 maxNum=maxX(maxX(t1,maxNum),t2);
41 --t1;
42 --t2;
43 ++mats[t1][t2];
44 ++degree[t1];
45 ++mats[t2][t1];
46 ++degree[t2];
47 }
48 int start =0 ;
49 for(int i = 0;i<maxNum;++i)
50 {
51 if(degree[i]%2 ==1)
52 {
53 start = i;
54 break;
55 }
56 }
57
58
59
60 int count=0;
61 find_path_u(maxNum,mats,start,count,path);
62
63 for(int i = count-1;i>=0;--i)
64 {
65 fprintf(fout,"%d\n",path[i]+1);
66 }
67 fclose(fin);
68 fclose(fout);
69 return 0;
70 }
posted @ 2011-05-28 12:21  幻魇  阅读(187)  评论(0)    收藏  举报