Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define isz(x) (int)(x).size()
- struct Edge {
- int u, v, cost;
- };
- const int NMAX = 20020, INF = (int)1e9+7;
- int nV;
- std::vector<Edge> edges;
- std::vector<int> adj[NMAX];
- int dist[NMAX], from[NMAX];
- void find_path(int s, int t, bool first) {
- std::fill(dist,dist+NMAX,INF);
- std::fill(from,from+NMAX, -1);
- std::deque<int> q = {s};
- dist[s] = 0;
- while (isz(q)) {
- auto u = q.front(); q.pop_front();
- for (int i : adj[u]) {
- const auto & e = edges[i];
- int v = e.u + e.v - u;
- if (dist[v] > dist[u] + e.cost) {
- if (e.cost == 0 && !first) { q.push_front(v); }
- else { q.push_back(v); }
- dist[v] = dist[u] + e.cost;
- from[v] = i;
- }
- }
- }
- while (from[t] != -1) {
- int id = from[t];
- auto &e = edges[id];
- e.cost++;
- t = e.u + e.v - t;
- }
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(0);
- freopen("inevit.in", "rt", stdin);
- freopen("inevit.out", "wt", stdout);
- int nE; std::cin >> nV >> nE;
- for (int i = 0, u, v; i < nE; i++) {
- std::cin >> u >> v;
- edges.push_back(Edge{u,v,0});
- adj[u].push_back(i);
- adj[v].push_back(i);
- }
- find_path(1, nV, 1);
- find_path(1, nV, 0);
- int t = nV;
- std::vector<int> answ;
- while (from[t] != -1) {
- int id = from[t];
- const auto &e = edges[id];
- if (e.cost == 2) {
- answ.push_back(id + 1);
- }
- t = e.u + e.v - t;
- }
- std::reverse(answ.begin(),answ.end());
- std::cout << answ.size() << '\n';
- for (auto it : answ) std::cout << it << ' ';
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement