Task: | Longest route |
Sender: | aalto2024h_004 |
Submission time: | 2024-10-23 17:44:29 +0300 |
Language: | C++ (C++20) |
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 | WRONG ANSWER | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | WRONG ANSWER | 0.09 s | details |
#7 | ACCEPTED | 0.09 s | details |
#8 | ACCEPTED | 0.09 s | details |
#9 | ACCEPTED | 0.09 s | details |
#10 | ACCEPTED | 0.09 s | details |
#11 | ACCEPTED | 0.06 s | details |
#12 | ACCEPTED | 0.07 s | details |
#13 | ACCEPTED | 0.00 s | details |
#14 | ACCEPTED | 0.00 s | details |
#15 | ACCEPTED | 0.06 s | details |
#16 | ACCEPTED | 0.00 s | details |
#17 | ACCEPTED | 0.06 s | details |
#18 | ACCEPTED | 0.04 s | details |
#19 | ACCEPTED | 0.00 s | details |
#20 | ACCEPTED | 0.04 s | details |
#21 | ACCEPTED | 0.00 s | details |
Code
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <string> #include <limits> #define REP(i,a,b) for(int i = a; i < b; i++) // Type Aliases for 1D and 2D vectors with initialization #define vi(n, val) vector<int>(n, val) // 1D vector of ints with size n, initialized to val #define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val #define ll long long #define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val)) // 2D vector of ints(n x m), initialized to val #define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs(n x m), initialized to val #define LL_INF std::numeric_limits<long long int>::max() #define INF -1e9 using namespace std; template <typename T> void pV(const std::vector<T>& vec, const std::string& label = "Vector") { std::cout << label << ": [ "; for(const auto& elem : vec) { std::cout << elem << " "; } std::cout << "]" << std::endl; } void dfs(int s, vector<bool> *visited, vector<int>(*adj)[]) { if((*visited)[s]) return; (*visited)[s] = true; // process node s for(auto u:(*adj)[s]) { dfs(u, visited, adj); } } /* // DEPTH-FRIRST-SEARCH vector<int> adj[N]; vector<bool> visited(N, false); int u, v; for(int i = 0; i < M;i++){ cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } */ vector<long long> dijkstra(int N, int x, vector<vector<pair<int,int>>> *adj){ vector<bool> processed(N, false); vector<long long> distance(N, LL_INF); priority_queue<pair<long long,int>> q; //for(int i = 1; i <= n; i++) distance[i] = INF; distance[x] = 0; q.push({0,x}); while(!q.empty()) { int a = q.top().second; q.pop(); if(processed[a]) continue; processed[a] = true; for(auto u :(*adj)[a]) { int b = u.first, w = u.second; if(distance[a]+w < distance[b]) { distance[b] = distance[a]+w; q.push({-distance[b],b}); } } } return distance; } /* DIJKSTRA //vector<vector<pair<int,int>>> adj(N, vector<pair<int, int>>(N)); */ int main() { ios::sync_with_stdio(0); cin.tie(0); // Your code starts here ll n, m; cin >> n >> m; vector<vector<ll>> adj(n + 1); vector<ll> indegree(n + 1, 0); vector<ll> dp(n + 1, INF); vector<ll> parent(n + 1, -1); for(ll i = 0; i < m; i++) { ll a, b; cin >> a >> b; adj[a].push_back(b); indegree[b]++; } queue<ll> q; for(ll i = 1; i <= n; i++) { if(indegree[i] == 0) { q.push(i); } } dp[1] = 1; while(!q.empty()) { ll node = q.front(); q.pop(); for(ll next : adj[node]) { if(dp[node] + 1 > dp[next]) { dp[next] = dp[node] + 1; parent[next] = node; } indegree[next]--; if(indegree[next] == 0) { q.push(next); } } } if(dp[n] == INF) { cout << "IMPOSSIBLE" << endl; return 0; } cout << dp[n] << endl; stack<ll> path; ll current = n; while(current != -1) { path.push(current); current = parent[current]; } cout << path.top(); path.pop(); while(!path.empty()) { cout << " " << path.top(); path.pop(); } cout << endl; 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: WRONG ANSWER
input |
---|
10 10 8 10 2 6 2 10 7 10 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
-999999997 8 9 5 10 |
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 4 9 10 |
Test 6
Verdict: WRONG ANSWER
input |
---|
100000 200000 86085 57043 45527 29537 41919 84699 95993 82082 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
-999999954 29326 34588 81498 12317 87862 ... Truncated |
Test 7
Verdict: ACCEPTED
input |
---|
100000 200000 10961 53490 59843 36636 40674 66772 32618 41570 ... |
correct output |
---|
31 1 37239 44082 21537 90572 7332... |
user output |
---|
31 1 37239 44082 21537 90572 7332... Truncated |
Test 8
Verdict: ACCEPTED
input |
---|
100000 200000 87375 76468 38855 27547 49415 83191 38572 1524 ... |
correct output |
---|
35 1 91343 59014 56722 34054 3875... |
user output |
---|
35 1 91343 59014 56722 34054 3875... Truncated |
Test 9
Verdict: ACCEPTED
input |
---|
100000 200000 17973 70097 19982 80323 96486 2404 75650 63274 ... |
correct output |
---|
36 1 25685 90292 59380 91058 2663... |
user output |
---|
36 1 25685 90292 59380 91058 2663... Truncated |
Test 10
Verdict: ACCEPTED
input |
---|
100000 200000 74343 53088 97443 7885 64807 58252 9374 33312 ... |
correct output |
---|
28 1 26390 15278 11333 48479 6881... |
user output |
---|
28 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: ACCEPTED
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 |
---|
99999 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 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: ACCEPTED
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 |
---|
99999 1 3 2 5 4 7 6 9 8 11 10 13 12 ... 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 |