-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace3.cpp
executable file
·140 lines (128 loc) · 4.25 KB
/
race3.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
ID: paulius10
PROG: race3
LANG: C++
*/
#include <fstream>
#include <vector>
using namespace std;
vector<vector<int> > process_input_file() {
vector<vector<int> > adjacency_list;
ifstream fin("race3.in");
while (true) {
vector<int> list;
int point;
while (true) {
fin >> point;
if (point < 0) break;
list.push_back(point);
}
if (point == -1) break;
adjacency_list.push_back(list);
}
fin.close();
return adjacency_list;
}
void flood_fill(int new_component, int component[],
vector<vector<int> > adjacency_list) {
int num_visited;
do {
num_visited = 0;
for (int i = 0; i < adjacency_list.size(); i++) {
if (component[i] == -2) {
num_visited++;
component[i] = new_component;
for (vector<int>::iterator j = adjacency_list[i].begin();
j != adjacency_list[i].end(); j++) {
if (component[*j] == -1) component[*j] = -2;
}
}
}
} while (num_visited != 0);
}
vector<int> find_unavoidable_points(vector<vector<int> > adjacency_list) {
int size = adjacency_list.size();
vector<int> unavoidable;
for (int point = 1; point < size - 1; point++) {
int num_components = 0, component[size];
for (int i = 0; i < size; i++) component[i] = -1;
component[point] = 0;
for (int i = 0; i < size; i++) {
if (component[i] == -1) {
num_components++;
component[i] = -2;
flood_fill(num_components, component, adjacency_list);
}
}
if (num_components > 1) unavoidable.push_back(point);
}
return unavoidable;
}
void mark_reachable_points(int point, int finish, vector<bool> &visited,
vector<vector<int> > adjacency_list) {
if (visited[point]) return;
visited[point] = true;
for (vector<int>::iterator next_point = adjacency_list[point].begin();
next_point != adjacency_list[point].end(); next_point++) {
mark_reachable_points(*next_point, finish, visited, adjacency_list);
}
}
bool no_outgoing_arrows(int splitting_point, vector<int> outgoing,
vector<bool> first) {
for (vector<int>::iterator point = outgoing.begin(); point != outgoing.end();
point++) {
if (first[*point] && *point != splitting_point) return false;
}
return true;
}
bool no_common_arrows(vector<bool> first, vector<bool> second,
vector<vector<int> > adjacency_list) {
for (int i = 0; i < adjacency_list.size(); i++) {
for (vector<int>::iterator j = adjacency_list[i].begin();
j != adjacency_list[i].end(); j++) {
if (!((first[i] && first[*j]) || (second[i] && second[*j]))) return false;
}
}
return true;
}
bool no_common_points(int splitting_point, vector<bool> first,
vector<bool> second) {
for (int i = 0; i < first.size(); i++) {
if (first[i] == second[i] && i != splitting_point) return false;
}
return true;
}
vector<int> find_splitting_points(vector<int> unavoidable,
vector<vector<int> > adjacency_list) {
int size = adjacency_list.size();
vector<int> splitting;
for (vector<int>::iterator splitting_point = unavoidable.begin();
splitting_point != unavoidable.end(); splitting_point++) {
vector<bool> first(size, false), second(size, false);
first[*splitting_point] = second[size - 1] = true;
mark_reachable_points(0, *splitting_point, first, adjacency_list);
mark_reachable_points(*splitting_point, size - 1, second, adjacency_list);
if (no_outgoing_arrows(*splitting_point, adjacency_list[*splitting_point],
first) &&
no_common_arrows(first, second, adjacency_list) &&
no_common_points(*splitting_point, first, second)) {
splitting.push_back(*splitting_point);
}
}
return splitting;
}
void output(ofstream &fout, vector<int> v) {
fout << v.size();
for (int i = 0; i < v.size(); i++) fout << " " << v[i];
fout << endl;
}
int main() {
vector<vector<int> > adjacency_list = process_input_file();
vector<int> unavoidable = find_unavoidable_points(adjacency_list);
vector<int> splitting = find_splitting_points(unavoidable, adjacency_list);
ofstream fout("race3.out");
output(fout, unavoidable);
output(fout, splitting);
fout.close();
return 0;
}