#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.push({1, 0});
while (!q.empty()) {
auto [node, d] = q.front();
int t = (d + 1) % l;
if (node == n) {
cout << d << '\n';
return 0;
}
q.pop();
if (!visited[node] && !bad_node[t].count(node)) {
visited[node] = true;
q.push({node, d + 1});
}
for (int neighbor : graph[node]) {
if (!visited[neighbor] && !bad_node[t].count(neighbor)) {
if (!bad_edge[t].count({min(node, neighbor), max(node, neighbor)})) {
visited[neighbor] = true;
q.push({neighbor, d + 1});
}
}
}
}
// 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 |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
37 ms |
6216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
64 ms |
6220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
64 ms |
6220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
64 ms |
6220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
37 ms |
6216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
37 ms |
6216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
37 ms |
6216 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |