Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstdio>
- using namespace std;
- struct struc1{
- int64_t right, nowflow, link;
- bool used;
- };
- struct struc2{
- int64_t l, r, cost;
- };
- int64_t big_num = 1000000000, n, m, posque = 0;
- bool flg;
- vector<vector<struc1>> List;
- vector<int64_t> dist, parent;
- vector<struc2> edge;
- vector<bool> used;
- vector<vector<int64_t>> answer;
- vector<int64_t> que;
- int64_t min(int64_t o1, int64_t o2) {
- if (o1 < o2)
- return o1;
- return o2;
- }
- void dfs(){
- int vertex = que[posque++];
- //cout << vertex << "\n";
- for (int i = 0; i < List[vertex].size(); i++)
- if (!List[vertex][i].used)
- if (dist[List[vertex][i].right] > dist[vertex] + 1) {
- parent[List[vertex][i].right] = vertex;
- dist[List[vertex][i].right] = dist[vertex] + 1;
- que.push_back(List[vertex][i].right);
- }
- }
- void bfs() {
- while (posque < que.size())
- dfs();
- }
- int main() {
- int64_t k, numto, numfrom;
- cin >> n >> m >> numfrom >> numto;
- List.resize(n + 1);
- dist.resize(n + 1);
- parent.resize(n + 1);
- edge.resize(2 * m);
- used.resize(2 * m);
- for (int i = 0; i < m; i++) {
- int a, b;
- struc1 per1;
- cin >> a >> b;
- per1.nowflow = 0;
- per1.right = b;
- per1.link = -1;
- per1.used = false;
- List[a].push_back(per1);
- }
- /* for (int i = 1; i <= n; i++)
- for (int j = 0; j < List[i].size(); j++) {
- if (List[i][j].link < 0) {
- struc1 per1;
- per1.nowflow = 0;
- per1.right = i;
- per1.used = false;
- per1.link = j;
- List[i][j].link = List[List[i][j].right].size();
- List[List[i][j].right].push_back(per1);
- }
- }*/
- int64_t total_cost = 0;
- answer.resize(2);
- for (int i = 1; i <= 2; i++) {
- for (int j = 1; j <= n; j++)
- dist[j] = big_num;
- dist[numfrom] = 0;
- que.resize(0);
- que.push_back(numfrom);
- posque = 0;
- bfs();
- if (dist[n] == big_num) {
- cout << "NO";
- exit(0);
- }
- /*for (int ii = 1; ii <= n; ii++)
- cout << dist[ii] << " ";
- cout << "\n";
- for (int ii = 1; ii <= n; ii++)
- cout << parent[ii] << " ";
- cout << "\n";*/
- int64_t nowpos = numto;
- while (nowpos != numfrom) {
- int64_t l = parent[nowpos];
- for (int g = 0; g < List[l].size(); g++)
- if (List[l][g].right == nowpos && !List[l][g].used) {
- List[l][g].used = true;
- List[l][g].nowflow++;
- //
- if (List[l][g].link < 0) {
- struc1 per1;
- per1.nowflow = 0;
- per1.right = l;
- per1.used = false;
- per1.link = g;
- List[l][g].link = List[nowpos].size();
- List[nowpos].push_back(per1);
- }
- //
- List[List[l][g].right][List[l][g].link].nowflow--;
- //cout << "!" << l << " " << nowpos << " " << g << "\n";
- break;
- }
- //answer[i - 1].push_back(nowpos);
- nowpos = l;
- //cout << nowpos <<"\n";
- }
- }
- for (int64_t i = 0; i < 2; i++) {
- int64_t nowpos = numfrom;
- while (nowpos != numto) {
- for (int j = 0; j < List[nowpos].size(); j++) {
- if (List[nowpos][j].nowflow > 0) {
- //cout << nowpos << "\n";
- List[nowpos][j].nowflow--;
- nowpos = List[nowpos][j].right;
- answer[i].push_back(nowpos);
- //cout << nowpos << " " << j << "\n";
- break;
- }
- }
- }
- }
- cout << "YES\n";
- for (int j = 0; j < 2; j++) {
- cout << numfrom << " ";
- for (int i = 0; i < answer[j].size(); i++)
- cout << answer[j][i] << " ";
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement