CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:ZDHKLV
Submission time:2024-10-21 17:46:31 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.00 sdetails
#60.00 sdetails
#70.00 sdetails
#80.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#120.00 sdetails
#130.00 sdetails
#140.00 sdetails
#15ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

long long max_flow(vector<vector<int>> &adj, vector<vector<long long>> &capacity,
                   int source, int sink) {

	int n = adj.size();
	vector<int> parent(n, -1);

	auto reachable = [&]() -> bool {
		queue<int> q;
		q.push(source);
		while (!q.empty()) {
			int node = q.front();
			q.pop();
			for (auto y : adj[node]) {
				if (capacity[node][y] <= 0 || parent[y] != -1)
                    continue;
				parent[y] = node;
				q.push(y);
			}
		}
		return parent[sink] != -1;
	};

	long long flow = 0;

	while (reachable()) {

		int node = sink;
		long long current_flow = LONG_LONG_MAX;
		
        while (node != source) {
			current_flow = min(current_flow, capacity[parent[node]][node]);
			node = parent[node];
		}

		node = sink;

		while (node != source) {
			capacity[parent[node]][node] -= current_flow;
			capacity[node][parent[node]] += current_flow;
			node = parent[node];
		}

		flow += current_flow;
		fill(parent.begin(), parent.end(), -1);

	}

	return flow;

}

void dfs(vector<bool> &visited, stack<int> &path, vector<vector<int>> &adj, int current, int dest) {

    visited[current] = true;

    path.push(current);
 
    if (current == dest) {
        stack<int> q = path;
        cout << q.size() << endl;
        while (!q.empty()) {
            int c = q.top();
            q.pop();
            cout << c+1 << " ";
        }
        cout << endl;
    } else {
        for (int next : adj[current]) {
            if (!visited[next]) {
                dfs(visited, path, adj, next, dest);
            }
        }
    }

    path.pop();
    visited[current] = false;

}

int main() {

	int n, m;
	cin >> n >> m;

	vector<vector<long long>> capacity(n, vector<long long>(n, 0));
	vector<vector<int>> adj(n);

	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
		capacity[a][b] += 1;
	}

    vector<vector<long long>> init_cap = capacity;

    long long k = max_flow(adj, capacity, 0, n - 1);
	cout << k << endl;

    vector<vector<int>> adj2(n);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (capacity[i][j] == 0) continue;

            if (init_cap[i][j] != 0) continue;
            adj2[i].push_back(j);
            
        }
    }

    vector<bool> visited(n, false);
    vector<vector<int>> paths(k);

    stack<int> path;
    dfs(visited, path, adj2, n-1, 0);
    
}

/*
6 7
1 2
1 3
2 6
3 4
3 5
4 6
5 6
*/

Test details

Test 1

Verdict: ACCEPTED

input
2 1
1 2

correct output
1
2
1 2 

user output
1
2
1 2 

Test 2

Verdict: ACCEPTED

input
4 2
1 2
3 4

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
500 996
1 2
2 500
1 3
3 500
...

correct output
498
3
1 2 500 
3
1 3 500 
...

user output
498
3
1 2 500 
3
1 3 500 
...
Truncated

Test 4

Verdict: ACCEPTED

input
500 499
1 2
2 3
3 4
4 5
...

correct output
1
500
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1
500
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 5

Verdict: ACCEPTED

input
2 1
2 1

correct output
0

user output
0

Test 6

Verdict:

input
40 1000
25 22
15 24
7 33
16 32
...

correct output
21
44
1 35 39 34 29 32 22 38 20 30 1...

user output
21
3
1 6 40 
3
1 10 40 
...

Test 7

Verdict:

input
75 1000
72 6
46 66
63 45
70 46
...

correct output
12
30
1 29 24 9 18 63 45 31 66 72 6 ...

user output
12
4
1 39 6 75 
4
1 19 8 75 
...
Truncated

Test 8

Verdict:

input
100 1000
75 97
7 62
88 25
36 44
...

correct output
9
51
1 35 15 86 79 34 43 94 83 75 9...

user output
9
2
1 100 
4
1 35 23 100 
...

Test 9

Verdict: ACCEPTED

input
3 2
1 2
2 3

correct output
1
3
1 2 3 

user output
1
3
1 2 3 

Test 10

Verdict: ACCEPTED

input
11 12
1 2
2 3
3 4
4 5
...

correct output
2
6
1 2 3 4 5 11 
7
1 6 7 8 9 10 11 

user output
2
6
1 2 3 4 5 11 
7
1 6 7 8 9 10 11 

Test 11

Verdict: ACCEPTED

input
8 9
1 2
2 3
3 8
1 4
...

correct output
2
5
1 2 6 7 8 
5
1 4 5 3 8 

user output
2
5
1 4 5 3 8 
5
1 2 6 7 8 

Test 12

Verdict:

input
8 9
1 2
1 3
2 3
3 4
...

correct output
1
8
1 2 3 4 5 6 7 8 

user output
1

Test 13

Verdict:

input
7 9
1 2
1 3
2 7
3 4
...

correct output
3
3
1 2 7 
4
1 3 5 7 
...

user output
3
3
1 2 7 
4
1 3 4 7 
...

Test 14

Verdict:

input
7 15
3 6
5 2
5 4
3 5
...

correct output
2
5
1 2 3 6 7 
4
1 4 5 7 

user output
2

Test 15

Verdict: ACCEPTED

input
500 986
244 252
224 22
81 484
273 432
...

correct output
116
5
1 129 142 473 500 
5
1 63 158 171 500 
...

user output
116
5
1 399 446 5 500 
5
1 122 457 6 500 
...
Truncated