forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
107 lines (94 loc) · 3 KB
/
C.cpp
File metadata and controls
107 lines (94 loc) · 3 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 2011;
vector<int> ke[MN];
int n, m, A, B, C, D;
int tru[MN][MN], trv[MN][MN];
vector<int> res[2];
void go(int u, int v) {
if (u == 0 && v == 0) return ;
go(tru[u][v], trv[u][v]);
if (u != tru[u][v]) res[0].push_back(u);
if (v != trv[u][v]) res[1].push_back(v);
}
void solve() {
queue<int> qu, qv;
memset(tru, -1, sizeof tru);
qu.push(0);
qv.push(0);
tru[0][0] = 0;
trv[0][0] = 0;
while (!qu.empty()) {
int u = qu.front(); qu.pop();
int v = qv.front(); qv.pop();
if (u == B && v == D) {
res[0].clear(); res[1].clear();
go(B, D);
cout << "YES" << endl;
for(int x : res[0]) cout << x << ' '; cout << endl;
for(int x : res[1]) cout << x << ' '; cout << endl;
return ;
}
// (u, v) --> (u, vv)
if (v != D) {
auto it = upper_bound(ke[v].begin(), ke[v].end(), u);
while (it != ke[v].end()) {
int vv = *it;
++it;
if (v == 0 && vv != C) continue;
if (tru[u][vv] < 0) {
tru[u][vv] = u;
trv[u][vv] = v;
qu.push(u);
qv.push(vv);
}
}
}
// (u, v) --> (uu, v)
if (u != B) {
auto it = upper_bound(ke[u].begin(), ke[u].end(), v);
while (it != ke[u].end()) {
int uu = *it;
++it;
if (u == 0 && uu != A) continue;
if (tru[uu][v] < 0) {
tru[uu][v] = u;
trv[uu][v] = v;
qu.push(uu);
qv.push(v);
}
}
}
}
cout << "NO" << endl;
}
int main() {
freopen("express.in", "r", stdin);
freopen("express.out", "w", stdout);
while (scanf("%d%d", &n, &m) == 2) {
scanf("%d%d%d%d", &A, &B, &C, &D);
if (A > B) swap(A, B);
if (C > D) swap(C, D);
FOR(i,0,n) ke[i].clear();
FOR(i,1,m) {
int u, v; scanf("%d%d", &u, &v);
ke[u].push_back(v);
}
ke[0].push_back(A);
ke[0].push_back(C);
FOR(i,0,n) {
sort(ke[i].begin(), ke[i].end());
ke[i].resize(unique(ke[i].begin(), ke[i].end()) - ke[i].begin());
}
solve();
}
}