-
Notifications
You must be signed in to change notification settings - Fork 0
/
fence.cpp
83 lines (67 loc) · 1.48 KB
/
fence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
ID: pmh1921
PROG: fence
LANG: C++11
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
int F;
vector<int> path;
void findPath(int loc, vector<int>* adjList) {
if (adjList[loc].empty()) {
path.push_back(loc);
}
else {
while (!adjList[loc].empty()) {
int lowest = 0;
for (int i = 1; i < adjList[loc].size(); i++) { if (adjList[loc][i] < adjList[loc][lowest]) { lowest = i; } }
int val = adjList[loc][lowest];
adjList[loc].erase(adjList[loc].begin() + lowest);
vector<int>::iterator f = find(adjList[val].begin(), adjList[val].end(), loc);
adjList[val].erase(f);
findPath(val, adjList);
}
path.push_back(loc);
}
}
int main() {
ofstream fout ("fence.out");
ifstream fin ("fence.in");
vector<int> adjList[501];
vector<int> odds;
bool isOdd[501] = {0};
fin >> F;
for (int i = 0; i < F; i++) {
int a, b;
fin >> a >> b;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
for (int i = 1; i <= 500; i++) {
int neighbours = adjList[i].size();
if (neighbours == 0) { continue; }
if (neighbours % 2 == 1) {
odds.push_back(i);
isOdd[i] = true;
}
}
int lowest = 501;
for (int i = 1; i <= 500; i++) {
if (adjList[i].size() == 0) { continue; }
if (odds.size() == 0 || isOdd[i]) {
lowest = i;
break;
}
}
findPath(lowest, adjList);
for (int i = path.size()-1; i >= 0; i--) {
fout << path[i] << endl;
}
return 0;
}