-
Notifications
You must be signed in to change notification settings - Fork 0
/
zi_snails_old.cpp
106 lines (82 loc) · 1.85 KB
/
zi_snails_old.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
#include <cstdio>
#include <list>
using namespace std;
//int const maxObjectNumber = 100001;
int const maxObjectNumber = 22001;
int n;
int m;
int a;
int h;
list<int> edges[maxObjectNumber];
int f[maxObjectNumber][maxObjectNumber];
int c[maxObjectNumber][maxObjectNumber];
list< pair<int, int> > resultEdges;
list<int> resultGraph[maxObjectNumber];
bool dfs(int v) {
if (v == h) {
return true;
}
for (list<int>::iterator it = edges[v].begin(); it != edges[v].end(); it++) {
int x = *it;
if (f[v][x] < c[v][x])
{
f[v][x]++;
if (dfs(x)) {
resultEdges.push_back(pair<int, int>(v, x));
if (c[x][v] == 0) {
c[x][v] = 1;
edges[x].push_back(v);
}
return true;
}
f[v][x]--;
}
}
return false;
}
int main() {
FILE* in = fopen("snails.in", "r");
fscanf(in, "%d %d %d %d", &n, &m, &a, &h);
for (int i = 0; i < m; i++) {
int x;
int y;
fscanf(in, "%d %d", &x, &y);
edges[x].push_back(y);
c[x][y] = 1;
}
fclose(in);
bool result = true;
result = result && dfs(a);
result = result && dfs(a);
for (list< pair<int, int> >::iterator it = resultEdges.begin();
it != resultEdges.end(); it++) {
int x = it->first;
int y = it->second;
if (f[y][x] == 0) {
//не использовалось обратное ребро
resultGraph[x].push_back(y);
}
}
FILE* out = fopen("snails.out", "w");
if (result) {
fprintf(out, "YES\n");
for (list<int>::iterator it = resultGraph[a].begin();
it != resultGraph[a].end(); it++) {
fprintf(out, "%d ", a);
list<int>::iterator curPlain = it;
while (true) {
fprintf(out, "%d ", *curPlain);
int curPlainNum = *curPlain;
curPlain = resultGraph[curPlainNum].begin();
if (curPlain == resultGraph[curPlainNum].end()) {
break;
}
}
fprintf(out, "\n");
}
} else {
fprintf(out, "NO");
}
fclose(out);
return 0;
}