#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 = 1000;
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
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2004 KB |
Output is correct |
2 |
Correct |
525 ms |
8112 KB |
Output is correct |
3 |
Correct |
528 ms |
8440 KB |
Output is correct |
4 |
Correct |
679 ms |
9316 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
561 ms |
8992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2004 KB |
Output is correct |
2 |
Correct |
515 ms |
8112 KB |
Output is correct |
3 |
Correct |
530 ms |
8356 KB |
Output is correct |
4 |
Correct |
743 ms |
9284 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
568 ms |
9124 KB |
Output is correct |
7 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2004 KB |
Output is correct |
2 |
Correct |
515 ms |
8112 KB |
Output is correct |
3 |
Correct |
530 ms |
8356 KB |
Output is correct |
4 |
Correct |
743 ms |
9284 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
568 ms |
9124 KB |
Output is correct |
7 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
2004 KB |
Output is correct |
2 |
Correct |
515 ms |
8112 KB |
Output is correct |
3 |
Correct |
530 ms |
8356 KB |
Output is correct |
4 |
Correct |
743 ms |
9284 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
568 ms |
9124 KB |
Output is correct |
7 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2004 KB |
Output is correct |
2 |
Correct |
525 ms |
8112 KB |
Output is correct |
3 |
Correct |
528 ms |
8440 KB |
Output is correct |
4 |
Correct |
679 ms |
9316 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
561 ms |
8992 KB |
Output is correct |
7 |
Correct |
11 ms |
2004 KB |
Output is correct |
8 |
Correct |
515 ms |
8112 KB |
Output is correct |
9 |
Correct |
530 ms |
8356 KB |
Output is correct |
10 |
Correct |
743 ms |
9284 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
568 ms |
9124 KB |
Output is correct |
13 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2004 KB |
Output is correct |
2 |
Correct |
525 ms |
8112 KB |
Output is correct |
3 |
Correct |
528 ms |
8440 KB |
Output is correct |
4 |
Correct |
679 ms |
9316 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
561 ms |
8992 KB |
Output is correct |
7 |
Correct |
11 ms |
2004 KB |
Output is correct |
8 |
Correct |
515 ms |
8112 KB |
Output is correct |
9 |
Correct |
530 ms |
8356 KB |
Output is correct |
10 |
Correct |
743 ms |
9284 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
568 ms |
9124 KB |
Output is correct |
13 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2004 KB |
Output is correct |
2 |
Correct |
525 ms |
8112 KB |
Output is correct |
3 |
Correct |
528 ms |
8440 KB |
Output is correct |
4 |
Correct |
679 ms |
9316 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
561 ms |
8992 KB |
Output is correct |
7 |
Correct |
11 ms |
2004 KB |
Output is correct |
8 |
Correct |
515 ms |
8112 KB |
Output is correct |
9 |
Correct |
530 ms |
8356 KB |
Output is correct |
10 |
Correct |
743 ms |
9284 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
568 ms |
9124 KB |
Output is correct |
13 |
Runtime error |
32 ms |
12284 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |