#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
const int MAX_N = 1e5 + 5;
const int MAX_L = 130;
int bad_node[MAX_L];
pii bad_edge[MAX_L];
queue<pii> q[MAX_L];
bitset<MAX_N> visited[MAX_L];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vvi graph(n + 1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int k, l;
cin >> k >> l;
vi path(l + 1);
for (int i = 0; i < l; i++) {
cin >> path[i];
bad_node[i] = path[i];
}
path[l] = path[0];
for (int i = 1; i <= l; i++) {
bad_edge[i] = {min(path[i], path[i - 1]), max(path[i], path[i - 1])};
}
bad_edge[0] = bad_edge[l];
q[0].push({1, 0});
for (int i = 0; ; i++) {
if (i == l) i = 0;
if (q[i].empty()) break;
int t = (i + 1) % l;
while (!q[i].empty()) {
auto [node, d] = q[i].front();
if (node == n) {
cout << d << '\n';
return 0;
}
q[i].pop();
if (!visited[t][node] && bad_node[t] != node) {
visited[t][node] = true;
q[t].push({node, d + 1});
}
for (int neighbor : graph[node]) {
if (!visited[t][neighbor] && bad_node[t] != neighbor) {
if (bad_edge[t] != pii{min(node, neighbor), max(node, neighbor)}) {
visited[t][neighbor] = true;
q[t].push({neighbor, d + 1});
}
}
}
}
}
cout << "impossible\n";
return 0;
}
//6 6
//1 2
//2 3
//3 4
//4 5
//5 2
//5 6
//1
//4 3 2 5 4
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1876 KB |
Output is correct |
2 |
Correct |
400 ms |
8656 KB |
Output is correct |
3 |
Correct |
423 ms |
8900 KB |
Output is correct |
4 |
Correct |
546 ms |
9812 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
456 ms |
9524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1900 KB |
Output is correct |
2 |
Correct |
405 ms |
8544 KB |
Output is correct |
3 |
Correct |
426 ms |
8820 KB |
Output is correct |
4 |
Correct |
550 ms |
9912 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
448 ms |
9528 KB |
Output is correct |
7 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1900 KB |
Output is correct |
2 |
Correct |
405 ms |
8544 KB |
Output is correct |
3 |
Correct |
426 ms |
8820 KB |
Output is correct |
4 |
Correct |
550 ms |
9912 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
448 ms |
9528 KB |
Output is correct |
7 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1900 KB |
Output is correct |
2 |
Correct |
405 ms |
8544 KB |
Output is correct |
3 |
Correct |
426 ms |
8820 KB |
Output is correct |
4 |
Correct |
550 ms |
9912 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
448 ms |
9528 KB |
Output is correct |
7 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1876 KB |
Output is correct |
2 |
Correct |
400 ms |
8656 KB |
Output is correct |
3 |
Correct |
423 ms |
8900 KB |
Output is correct |
4 |
Correct |
546 ms |
9812 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
456 ms |
9524 KB |
Output is correct |
7 |
Correct |
11 ms |
1900 KB |
Output is correct |
8 |
Correct |
405 ms |
8544 KB |
Output is correct |
9 |
Correct |
426 ms |
8820 KB |
Output is correct |
10 |
Correct |
550 ms |
9912 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
448 ms |
9528 KB |
Output is correct |
13 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1876 KB |
Output is correct |
2 |
Correct |
400 ms |
8656 KB |
Output is correct |
3 |
Correct |
423 ms |
8900 KB |
Output is correct |
4 |
Correct |
546 ms |
9812 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
456 ms |
9524 KB |
Output is correct |
7 |
Correct |
11 ms |
1900 KB |
Output is correct |
8 |
Correct |
405 ms |
8544 KB |
Output is correct |
9 |
Correct |
426 ms |
8820 KB |
Output is correct |
10 |
Correct |
550 ms |
9912 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
448 ms |
9528 KB |
Output is correct |
13 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1876 KB |
Output is correct |
2 |
Correct |
400 ms |
8656 KB |
Output is correct |
3 |
Correct |
423 ms |
8900 KB |
Output is correct |
4 |
Correct |
546 ms |
9812 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
456 ms |
9524 KB |
Output is correct |
7 |
Correct |
11 ms |
1900 KB |
Output is correct |
8 |
Correct |
405 ms |
8544 KB |
Output is correct |
9 |
Correct |
426 ms |
8820 KB |
Output is correct |
10 |
Correct |
550 ms |
9912 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
448 ms |
9528 KB |
Output is correct |
13 |
Incorrect |
203 ms |
7700 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |