Skip to content

Commit 0c25b96

Browse files
committed
accepted
1 parent 40046b2 commit 0c25b96

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

Online Judges/CSES/MessageRoute.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <queue>
4+
5+
6+
using namespace std;
7+
vector<vector<int> > g;
8+
vector<int>parent;
9+
10+
int bfs(int from, int to) {
11+
vector<int>dist(to+1, -1);
12+
parent.assign(to+1, -1);
13+
queue<int>q;
14+
q.push(from);
15+
dist[from] = 0;
16+
while(!q.empty()) {
17+
int u = q.front();
18+
q.pop();
19+
20+
for(int i = 0; i < (int)g[u].size(); i++) {
21+
if (dist[g[u][i]] == -1) {
22+
q.push(g[u][i]);
23+
dist[g[u][i]] = dist[u] + 1;
24+
parent[g[u][i]] = u;
25+
}
26+
}
27+
}
28+
return dist[to];
29+
}
30+
31+
int main() {
32+
int n, m, x, y;
33+
cin >> n >> m;
34+
g.assign(n, vector<int>());
35+
while(m--) {
36+
cin >> x >> y;
37+
--x; --y;
38+
g[x].push_back(y);
39+
g[y].push_back(x);
40+
}
41+
int res = bfs(0, --n);
42+
if (res != -1) {
43+
cout << res+1 << endl;
44+
int aux = parent[n];
45+
vector<int>ans;
46+
ans.push_back(n+1);
47+
while(aux != -1) {
48+
ans.push_back(aux+1);
49+
aux = parent[aux];
50+
}
51+
for (int i = (int)ans.size() - 1; i >= 0; i--) {
52+
if (i != (int)ans.size() - 1)
53+
cout << " ";
54+
cout << ans[i];
55+
}
56+
cout << endl;
57+
} else {
58+
cout << "IMPOSSIBLE\n";
59+
}
60+
return 0;
61+
}

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33

44
### ➜ ONLINE JUDGES
55
1. **AtCoder**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/AtCoder).
6-
1. **Code Jam - Google's Coding Competitions**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/Code%20Jam).
7-
2. **CodeChef**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/CodeChef).
8-
3. **CodeForces**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/CodeForces).
9-
4. **HackerEarth**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/HackerEarth).
10-
5. **Neps Academy**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/Neps%20Academy).
11-
6. **PKU JudgeOnline (POJ)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/POJ).
12-
7. **Sphere Online Judge (SPOJ)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/SPOJ).
13-
8. **URI Online Judge (URI)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/URI).
14-
9. **URI Online Judge (URI)**: [SQL Solutions](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/URI%20SQL%20Submissions).
15-
10. **UVA Online Judge (UVA)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/UVA).
6+
2. **Code Jam - Google's Coding Competitions**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/Code%20Jam).
7+
3. **CodeChef**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/CodeChef).
8+
4. **CodeForces**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/CodeForces).
9+
5. **CSES**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/CSES).
10+
6. **HackerEarth**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/HackerEarth).
11+
7. **Neps Academy**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/Neps%20Academy).
12+
8. **PKU JudgeOnline (POJ)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/POJ).
13+
9. **Sphere Online Judge (SPOJ)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/SPOJ).
14+
10. **URI Online Judge (URI)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/URI).
15+
11. **URI Online Judge (URI)**: [SQL Solutions](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/URI%20SQL%20Submissions).
16+
12. **UVA Online Judge (UVA)**: [Solved Problems](https://github.com/AnneLivia/Competitive-Programming/tree/master/Online%20Judges/UVA).
1617

1718
### ➜ DATA STRUCTURES
1819
1. **Deque**: [Implementations](https://github.com/AnneLivia/Competitive-Programming/tree/master/Data%20Structures/Deque).

0 commit comments

Comments
 (0)