-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.cpp
More file actions
98 lines (85 loc) · 2.11 KB
/
new.cpp
File metadata and controls
98 lines (85 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define endl '\n'
#define sz(x) (int)x.size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define prec(x) fixed<<setprecision(x)
#define testcase cout << "Case " << cs++ << ":"
//stol(s);sqrtl() to_string(x);
template <typename T>
using minHeap = priority_queue<T, vector<T>, greater<T>>;
#define unsyncIO ios_base::sync_with_stdio(false); cin.tie(nullptr)
const ld PI = acos((ld) - 1);
const int MOD = 1e9 + 7;
const ll INF = 2e18 + 1;
const ld EPS = 1e-9;
const int MX = 2e6;
int cs = 1;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
void solve(){
int n, m; cin >> n >> m;
vector <int> g[n+1];
vector <int> in(n+1, 0);
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
g[u].push_back(v);
in[v]++;
}
queue <int> q;
for(int i = 2; i <= n; i++) if(in[i] == 0) q.push(i);
while(!q.empty()){
int u = q.front(); q.pop();
for(auto &v : g[u]){
in[v]--;
if(v == 1) continue;
if(in[v] == 0) q.push(v);
}
}
vector <int> dist(n+1, 0), par(n+1, 0);
q.push(1);
dist[1] = 1;
par[1] = 1;
while(!q.empty()){
int u = q.front(); q.pop();
for(auto &v : g[u]){
in[v]--;
if(dist[v] < dist[u] + 1){
dist[v] = dist[u] + 1;
par[v] = u;
}
if(in[v] == 0) q.push(v);
}
}
if(dist[n]) {
int x = n;
vector <int> path;
while(x != 1){
path.push_back(x);
x = par[x];
}
path.push_back(1);
reverse(all(path));
cout << dist[n] << endl;
for(auto it : path) cout << it << ' ';
cout << endl;
}
else cout << "IMPOSSIBLE" << endl;
}
int main() {
unsyncIO;
int t = 1;
// cin >> t;
//cin.ignore();
while (t--) {
solve();
}
return 0;
}