#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<pair<int, int>> used_V(n, {-1, -1});
map<pair<int, int>, pair<int, int>> used_E;
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int L;
cin >> L;
vector<int> cycle(L);
for (int &v : cycle) {
cin >> v;
v--;
}
for (int i = 0; i < L; i++) {
used_V[cycle[i]] = {i, L};
int x = cycle[i], y = cycle[i + 1 < L ? i + 1 : 0];
used_E[{x, y}] = used_E[{y, x}] = {i, L};
}
}
vector d(n, vector<int>(2, INT_MAX));
queue<pair<int, int>> q;
q.push({0, 0});
d[0][0] = 0;
while (!q.empty()) {
auto [u, r] = q.front();
q.pop();
bool can_stay = true;
for (int v : g[u]) {
if (used_E.count({u, v}) && d[u][r] % used_E[{u, v}].second == used_E[{u, v}].first) {
can_stay = false;
continue;
}
if (used_V[v].first != -1 && (d[u][r] + 1) % used_V[v].second == used_V[v].first) {
continue;
}
if (d[v][0] == INT_MAX) {
d[v][0] = d[u][r] + 1;
q.push({v, 0});
}
}
if (can_stay && r == 0) {
d[u][1] = d[u][0] + 1;
q.push({u, 1});
}
}
cout << d[n - 1][0] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1824 KB |
Output is correct |
2 |
Incorrect |
51 ms |
13188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1828 KB |
Output is correct |
2 |
Incorrect |
59 ms |
13132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1828 KB |
Output is correct |
2 |
Incorrect |
59 ms |
13132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1828 KB |
Output is correct |
2 |
Incorrect |
59 ms |
13132 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1824 KB |
Output is correct |
2 |
Incorrect |
51 ms |
13188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1824 KB |
Output is correct |
2 |
Incorrect |
51 ms |
13188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1824 KB |
Output is correct |
2 |
Incorrect |
51 ms |
13188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |