Task: | Longest route |
Sender: | aalto2024h_003 |
Submission time: | 2024-10-23 16:48:46 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | WRONG ANSWER |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.12 s | details |
#7 | WRONG ANSWER | 0.12 s | details |
#8 | WRONG ANSWER | 0.12 s | details |
#9 | WRONG ANSWER | 0.12 s | details |
#10 | WRONG ANSWER | 0.12 s | details |
#11 | ACCEPTED | 0.07 s | details |
#12 | ACCEPTED | 0.08 s | details |
#13 | ACCEPTED | 0.00 s | details |
#14 | ACCEPTED | 0.00 s | details |
#15 | WRONG ANSWER | 0.07 s | details |
#16 | ACCEPTED | 0.00 s | details |
#17 | WRONG ANSWER | 0.06 s | details |
#18 | ACCEPTED | 0.04 s | details |
#19 | ACCEPTED | 0.00 s | details |
#20 | ACCEPTED | 0.06 s | details |
#21 | ACCEPTED | 0.00 s | details |
Code
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); // RTE if input wrong datatype int n, m; cin >> n >> m; vector<vector<int>> adj(n), radj(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); radj[v].push_back(u); } vector<int> d(n, -1), p(n, -1); queue<int> q; d[0] = 0; q.push(0); while (sz(q)) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (d[v] == -1) { d[v] = d[u] + 1; p[v] = u; q.push(v); } } } while (sz(q)) q.pop(); vector<int> rd(n, -1), rp(n, -1); rd[n - 1] = 0; q.push(n - 1); while (sz(q)) { int u = q.front(); q.pop(); for (int v : radj[u]) { if (rd[v] == -1) { rd[v] = rd[u] + 1; rp[v] = u; q.push(v); } } } int best = -1, ans = -1; for (int i = 0; i < n; i++) { if (d[i] != -1 && rd[i] != -1 && d[i] + rd[i] > best) { best = d[i] + rd[i]; ans = i; } } if (ans == -1) { cout << "IMPOSSIBLE"; } else { vector<int> path; int u = ans; while (u != 0) { path.push_back(u); u = p[u]; } path.push_back(0); reverse(all(path)); vector<int> rpath; u = ans; while (u != n - 1) { rpath.push_back(u); u = rp[u]; } rpath.push_back(n - 1); rpath.erase(rpath.begin()); for (int i : rpath) path.push_back(i); cout << sz(path) << '\n'; for (int i : path) cout << i + 1 << ' '; } }
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: ACCEPTED
input |
---|
100000 200000 86085 57043 45527 29537 41919 84699 95993 82082 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 7
Verdict: WRONG ANSWER
input |
---|
100000 200000 10961 53490 59843 36636 40674 66772 32618 41570 ... |
correct output |
---|
31 1 37239 44082 21537 90572 7332... |
user output |
---|
21 1 37239 44082 21537 90572 7332... Truncated |
Test 8
Verdict: WRONG ANSWER
input |
---|
100000 200000 87375 76468 38855 27547 49415 83191 38572 1524 ... |
correct output |
---|
35 1 91343 59014 56722 34054 3875... |
user output |
---|
21 1 91343 59014 56722 34054 3875... Truncated |
Test 9
Verdict: WRONG ANSWER
input |
---|
100000 200000 17973 70097 19982 80323 96486 2404 75650 63274 ... |
correct output |
---|
36 1 25685 90292 59380 91058 2663... |
user output |
---|
25 1 25685 90292 59380 91058 2663... Truncated |
Test 10
Verdict: WRONG ANSWER
input |
---|
100000 200000 74343 53088 97443 7885 64807 58252 9374 33312 ... |
correct output |
---|
28 1 26390 15278 11333 48479 6881... |
user output |
---|
22 1 26390 15278 11333 48479 6881... Truncated |
Test 11
Verdict: ACCEPTED
input |
---|
100000 199998 1 100000 1 100000 2 100000 2 100000 ... |
correct output |
---|
2 1 100000 |
user output |
---|
2 1 100000 |
Test 12
Verdict: ACCEPTED
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 |
---|
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... Truncated |
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: WRONG ANSWER
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 |
---|
50001 1 2 3 5 7 9 11 13 15 17 19 21 ... Truncated |
Test 16
Verdict: ACCEPTED
input |
---|
3 2 1 3 3 2 |
correct output |
---|
2 1 3 |
user output |
---|
2 1 3 |
Test 17
Verdict: WRONG ANSWER
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 |
---|
50001 1 3 2 4 6 8 10 12 14 16 18 20 ... Truncated |
Test 18
Verdict: ACCEPTED
input |
---|
100000 200000 1 2 1 3 1 4 1 5 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
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: ACCEPTED
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 |
---|
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... Truncated |
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 |