Submission details
Task:Longest route
Sender:aalto25h_007
Submission time:2025-10-22 17:39:28 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#4ACCEPTED0.00 sdetails
#50.00 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#110.14 sdetails
#12--details
#130.00 sdetails
#140.00 sdetails
#15--details
#160.00 sdetails
#17--details
#18ACCEPTED0.10 sdetails
#190.00 sdetails
#200.10 sdetails
#210.00 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:63:15: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
   63 |   while (curr != -1) {
      |          ~~~~~^~~~~
input/code.cpp:53:8: warning: unused variable 'success' [-Wunused-variable]
   53 |   char success = 0;
      |        ^~~~~~~

Code

#include <bits/stdc++.h>
#define ll unsigned long long

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1)
      os << ", ";
  }
  os << "]";
  return os;
}

std::pair<ll, ll> dfs(std::vector<std::vector<ll>> &connMap,
                      std::vector<ll> &parentMap, ll curr, ll end) {
  if (curr == end) {
    return std::pair<ll, ll>({0, 1});
  }

  ll maRet = 0;
  ll succ = 0;
  for (ll next : connMap[curr]) {
    std::pair<ll, ll> ret = dfs(connMap, parentMap, next, end);
    if (ret.first >= maRet && ret.second) {
      succ = ret.second;
      //   std::cout << "Pöö " << curr << std::endl;
      parentMap[next] = curr;
      maRet = ret.first;
    }
  }
  return std::pair<ll, ll>({maRet + 1, succ});
}

int main() {
  ll cityCount, flightCount;
  std::cin >> cityCount;
  std::cin >> flightCount;
  // std::vector<ll> heights(cityCount, -1);

  std::vector<std::vector<ll>> connMap(cityCount);
  for (ll ii = 0; ii < flightCount; ii++) {
    ll start, end;
    std::cin >> start;
    std::cin >> end;
    // connMap[start - 1].push_back(end - 1);
    connMap[end - 1].push_back({start - 1});
  }
  //   std::cout << connMap << std::endl;

  std::vector<ll> parentMap(cityCount, -1);
  char success = 0;
  std::pair<ll, ll> dfsRet = dfs(connMap, parentMap, cityCount - 1, 0);
  if (!dfsRet.second) {
    std::cout << "IMPOSSIBLE" << std::endl;
    return 0;
  }
  ll curr = 0;
  //   std::cout << "dfsRet " << dfsRet.first << std::endl;
  std::vector<ll> retThing;
  //   std::cout << parentMap << std::endl;
  while (curr != -1) {
    // std::cout << curr + 1 << " ";
    retThing.push_back(curr + 1);
    curr = parentMap[curr];
  }
  std::cout << retThing.size() << std::endl;
  for (int ii = retThing.size() - 1; ii > 0; ii--) {
    std::cout << retThing[ii] << " ";
  }
  std::cout << retThing[0] << std::endl;
}

Test details

Test 1

Verdict:

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

correct output
5
1 2 5 6 10 

user output
5
10 6 5 2 1

Test 2

Verdict:

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

correct output
4
1 2 8 10 

user output
4
10 8 2 1

Test 3

Verdict:

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

correct output
3
1 4 10 

user output
3
10 4 1

Test 4

Verdict: ACCEPTED

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

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 5

Verdict:

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

correct output
5
1 8 7 2 10 

user output
5
10 9 4 8 1

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
2
100000 1

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:

input
2 1
1 2

correct output
2
1 2 

user output
2
2 1

Test 14

Verdict:

input
5 4
1 2
2 3
3 4
1 5

correct output
2
1 5 

user output
2
5 1

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:

input
3 2
1 3
3 2

correct output
2
1 3 

user output
2
3 1

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: ACCEPTED

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

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 19

Verdict:

input
5 4
2 1
3 1
1 4
1 5

correct output
2
1 5 

user output
2
5 1

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
100000
100000 99999 99998 99997 99996...

Test 21

Verdict:

input
4 4
3 1
3 4
1 2
2 4

correct output
3
1 2 4 

user output
3
4 2 1