CSES - Aalto Competitive Programming 2024 - wk7 - Wed - Results
Submission details
Task:Longest route
Sender:aalto2024h_001
Submission time:2024-10-23 18:19:47 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#120.81 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#150.78 sdetails
#16ACCEPTED0.00 sdetails
#170.77 sdetails
#180.52 sdetails
#19ACCEPTED0.00 sdetails
#200.75 sdetails
#21ACCEPTED0.00 sdetails

Compiler report

input/code.cpp: In function 'int rec(int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<bool>&, std::vector<int>&)':
input/code.cpp:66:13: warning: variable 'prec' set but not used [-Wunused-but-set-variable]
   66 |         int prec = -2;
      |             ^~~~

Code

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

// Debug printing
#ifdef DEBUG
#define deb(fmt, args...) printf("DEBUG: %d: " fmt, __LINE__, ##args)
#else
#define deb(fmt, args...)
#endif

void print_array(vector<int> in, const string title = "Vector")
{
	cout << title << " [\n";
	for (const auto &el : in) {
		cout << el << " ";
	}
	cout << "\n] END\n";
}

void print_matrix(vector<vector<int> > in, const string title = "Matrix")
{
	cout << title << "[\n";
	for (unsigned int i = 0; i < in.size(); i++) {
		cout << "row " << i << ": ";
		for (unsigned int j = 0; j < in[i].size(); j++) {
			cout << in[i][j] << " ";
		}
		cout << "\n";
	}
	cout << "] END\n";
}

vector<int> top_sort;
bool impossible = false;

void top_dfs(int s, vector<int> &status, vector<vector<int> > adj)
{
	if (impossible)
		return;
	if (status[s] == 1) {
		impossible = true;
	}
	if (status[s] == 2) {
		return;
	};
	status[s] = 1;
	for (auto u : adj[s]) {
		top_dfs(u, status, adj);
	}
	status[s] = 2;
	top_sort.push_back(s);
}

int rec(int a, vector<vector<int> > &adj, vector<int> &path, vector<bool> &h,
	vector<int> &distances)
{
	if (a == 1)
		return 0;
	if (h[a])
		return distances[a];
	h[a] = true;
	int maximum = -2;
	int prec = -2;
	for (auto b : adj[a]) {
		int res = rec(b, adj, path, h, distances);
		// maximum = max(maximum, res);
		// maximum = max(maximum, distances[b].first);
		if (res > maximum) {
			maximum = res;
			prec = b;
			// maximum = distances[b].first;
		}
	}
	// distances[a] = {maximum, prec};
	// path.push_back(prec);
    distances[a] = maximum + 1;
	return maximum + 1;
}

int main(int argc, char *argv[])
{
	int n, m;
	cin >> n >> m;
	int tmp1, tmp2;
	vector<vector<int> > adj(n + 1);
	vector<vector<int> > adjrev(n + 1);
	for (int i = 0; i < m; i++) {
		cin >> tmp1 >> tmp2;
		adj[tmp1].push_back(tmp2);
		adjrev[tmp2].push_back(tmp1);
	}

	// print_matrix(adj);

	vector<int> status(n + 1, 0);
	for (int i = 1; i < n + 1; i++) {
		if (status[i] == 0)
			top_dfs(i, status, adj);
	}
	if (impossible) {
		cout << "IMPOSSIBLE\n";
	}
	// deb("top sort tehty\n");
	// print_array(top_sort, "sort");
	// print_matrix(adjrev, "rev");
	// reverse(top_sort.begin(), top_sort.end());
	vector<int> distances(n + 1, -1);
    distances[1] = 0;
	// for (auto vert : top_sort) {
	//     rec(vert, adjrev, distances);
	// }
	// for (auto p : distances) {
	//     deb("{ %d, %d }\n", p.first, p.second);
	// }
	vector<int> path;
	vector<bool> h(n + 1, false);
	rec(n, adjrev, path, h, distances);
    if (distances[n] == -1) {
        cout << "IMPOSSIBLE\n";
        return 0;
    }
    // print_array(distances, "dist");
    int v = n;
    int ma = 0;
    while (v != 1) {
        ma = -1;
        // deb("%d, ma: %d\n", v, ma);
        for (auto u : adjrev[v]) {
            if (distances[u] > ma) {
                ma = distances[u];
                v = u;
            }
        }
        if (ma == -1) {
            cout << "IMPOSSIBLE\n";
            return 0;
        }
        path.push_back(v);
    }
	// print_array(path, "path");
	cout << path.size() + 1 << "\n";
	for (int i = path.size() - 1; i >= 0; i--) {
		cout << path[i] << " ";
	}
	cout << n << "\n";
	// print_array(path);

	//handle top sorted stuff

	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
2 6
1 2
4 6
5 6
...

correct output
5
1 2 5 6 10 

user output
5
1 2 5 6 10

Test 2

Verdict: ACCEPTED

input
10 10
3 9
6 5
6 9
2 8
...

correct output
4
1 2 8 10 

user output
4
1 2 8 10

Test 3

Verdict: ACCEPTED

input
10 10
5 10
4 10
8 7
7 10
...

correct output
3
1 4 10 

user output
3
1 4 10

Test 4

Verdict: ACCEPTED

input
10 10
8 10
2 6
2 10
7 10
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 5

Verdict: ACCEPTED

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

correct output
5
1 8 7 2 10 

user output
5
1 8 7 2 10

Test 6

Verdict:

input
100000 200000
86085 57043
45527 29537
41919 84699
95993 82082
...

correct output
IMPOSSIBLE

user output
(empty)

Test 7

Verdict:

input
100000 200000
10961 53490
59843 36636
40674 66772
32618 41570
...

correct output
31
1 37239 44082 21537 90572 7332...

user output
(empty)

Test 8

Verdict:

input
100000 200000
87375 76468
38855 27547
49415 83191
38572 1524
...

correct output
35
1 91343 59014 56722 34054 3875...

user output
(empty)

Test 9

Verdict:

input
100000 200000
17973 70097
19982 80323
96486 2404
75650 63274
...

correct output
36
1 25685 90292 59380 91058 2663...

user output
(empty)

Test 10

Verdict:

input
100000 200000
74343 53088
97443 7885
64807 58252
9374 33312
...

correct output
28
1 26390 15278 11333 48479 6881...

user output
(empty)

Test 11

Verdict:

input
100000 199998
1 100000
1 100000
2 100000
2 100000
...

correct output
2
1 100000 

user output
(empty)

Test 12

Verdict:

input
100000 199998
1 2
1 2
2 3
2 3
...

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

user output
(empty)

Test 13

Verdict: ACCEPTED

input
2 1
1 2

correct output
2
1 2 

user output
2
1 2

Test 14

Verdict: ACCEPTED

input
5 4
1 2
2 3
3 4
1 5

correct output
2
1 5 

user output
2
1 5

Test 15

Verdict:

input
99999 149997
1 3
3 5
5 7
7 9
...

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

user output
(empty)

Test 16

Verdict: ACCEPTED

input
3 2
1 3
3 2

correct output
2
1 3 

user output
2
1 3

Test 17

Verdict:

input
99999 149997
1 2
2 4
4 6
6 8
...

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

user output
(empty)

Test 18

Verdict:

input
100000 200000
1 2
1 3
1 4
1 5
...

correct output
IMPOSSIBLE

user output
(empty)

Test 19

Verdict: ACCEPTED

input
5 4
2 1
3 1
1 4
1 5

correct output
2
1 5 

user output
2
1 5

Test 20

Verdict:

input
100000 99999
99999 100000
99998 99999
99997 99998
99996 99997
...

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

user output
(empty)

Test 21

Verdict: ACCEPTED

input
4 4
3 1
3 4
1 2
2 4

correct output
3
1 2 4 

user output
3
1 2 4