#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 = 5000;
int Gcd(int a, int b) {
return (b ? Gcd(b, a % b) : a);
}
inline int Lcm(int a, int b) {
return a * b / Gcd(a, b);
}
set<int> bad_node[MAX_L];
set<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;
cin >> k;
vi len(k);
vvi path(k);
int l = 1;
for (int j = 0; j < k; j++) {
cin >> len[j];
path[j].resize(len[j]);
l = Lcm(len[j], l);
for (int i = 0; i < len[j]; i++) {
cin >> path[j][i];
}
}
for (int j = 0; j < k; j++) {
// path[j][l] = path[j][0];
for (int i = 1; i <= l; i++) {
bad_node[i].insert(path[j][i % len[j]]);
bad_edge[i].insert({min(path[j][i % len[j]], path[j][(i - 1) % len[j]]), max(path[j][i % len[j]], path[j][(i - 1) % len[j]])});
}
}
swap(bad_node[0], bad_node[l]);
swap(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].count(node)) {
visited[t][node] = true;
q[t].push({node, d + 1});
}
for (int neighbor : graph[node]) {
if (!visited[t][neighbor] && !bad_node[t].count(neighbor)) {
if (!bad_edge[t].count({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 |
12 ms |
5076 KB |
Output is correct |
2 |
Correct |
499 ms |
11180 KB |
Output is correct |
3 |
Correct |
529 ms |
11496 KB |
Output is correct |
4 |
Correct |
666 ms |
12424 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
556 ms |
12188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5032 KB |
Output is correct |
2 |
Correct |
565 ms |
11124 KB |
Output is correct |
3 |
Correct |
523 ms |
11528 KB |
Output is correct |
4 |
Correct |
649 ms |
12392 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
561 ms |
12112 KB |
Output is correct |
7 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5032 KB |
Output is correct |
2 |
Correct |
565 ms |
11124 KB |
Output is correct |
3 |
Correct |
523 ms |
11528 KB |
Output is correct |
4 |
Correct |
649 ms |
12392 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
561 ms |
12112 KB |
Output is correct |
7 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5032 KB |
Output is correct |
2 |
Correct |
565 ms |
11124 KB |
Output is correct |
3 |
Correct |
523 ms |
11528 KB |
Output is correct |
4 |
Correct |
649 ms |
12392 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
561 ms |
12112 KB |
Output is correct |
7 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5076 KB |
Output is correct |
2 |
Correct |
499 ms |
11180 KB |
Output is correct |
3 |
Correct |
529 ms |
11496 KB |
Output is correct |
4 |
Correct |
666 ms |
12424 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
556 ms |
12188 KB |
Output is correct |
7 |
Correct |
14 ms |
5032 KB |
Output is correct |
8 |
Correct |
565 ms |
11124 KB |
Output is correct |
9 |
Correct |
523 ms |
11528 KB |
Output is correct |
10 |
Correct |
649 ms |
12392 KB |
Output is correct |
11 |
Correct |
3 ms |
4564 KB |
Output is correct |
12 |
Correct |
561 ms |
12112 KB |
Output is correct |
13 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5076 KB |
Output is correct |
2 |
Correct |
499 ms |
11180 KB |
Output is correct |
3 |
Correct |
529 ms |
11496 KB |
Output is correct |
4 |
Correct |
666 ms |
12424 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
556 ms |
12188 KB |
Output is correct |
7 |
Correct |
14 ms |
5032 KB |
Output is correct |
8 |
Correct |
565 ms |
11124 KB |
Output is correct |
9 |
Correct |
523 ms |
11528 KB |
Output is correct |
10 |
Correct |
649 ms |
12392 KB |
Output is correct |
11 |
Correct |
3 ms |
4564 KB |
Output is correct |
12 |
Correct |
561 ms |
12112 KB |
Output is correct |
13 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5076 KB |
Output is correct |
2 |
Correct |
499 ms |
11180 KB |
Output is correct |
3 |
Correct |
529 ms |
11496 KB |
Output is correct |
4 |
Correct |
666 ms |
12424 KB |
Output is correct |
5 |
Correct |
3 ms |
4564 KB |
Output is correct |
6 |
Correct |
556 ms |
12188 KB |
Output is correct |
7 |
Correct |
14 ms |
5032 KB |
Output is correct |
8 |
Correct |
565 ms |
11124 KB |
Output is correct |
9 |
Correct |
523 ms |
11528 KB |
Output is correct |
10 |
Correct |
649 ms |
12392 KB |
Output is correct |
11 |
Correct |
3 ms |
4564 KB |
Output is correct |
12 |
Correct |
561 ms |
12112 KB |
Output is correct |
13 |
Execution timed out |
6035 ms |
58444 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |