#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define what_is(a) cout << #a << " is " <<a << "\n"
#define checker(a) cout << "chekcer reached " << a << "\n"
inline void io(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
vector<vector<int>> adj(100000,vector<int>());
struct node{
int n,t,a;
};
void print(node n){
cout << n.n << " " << n.t << " " << n.a << "\n";
}
int mem[1000000][200];
int w[100000][200];
int L[100000];
void solve(){
int n,m;
cin >> n >> m;
for(int i=0;i<m;i++){
int u,v;
cin >>u >> v;
u--,v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
int k;
cin >> k;
while(k--){
int l; cin >> l;
vector<int> vec(l);
for(int& x: vec){
cin >> x;
x--;
}
for(int t=0;t<l;t++)
w[vec[t]][t] = 1,L[vec[t]] = l;
}
int ans = -1;
queue<node> q;
q.push({0,0,0});
while(!q.empty()){
node u = q.front();
//print(u);
q.pop();
if(u.n==n-1){
ans = u.a;
break;
}
for(int v:adj[u.n]){
if(L[v]!=0&&w[v][(u.t+1)%L[v]])continue;
if(L[u.n]!=0&&L[v]!=0&&w[u.n][(u.t+1)%L[u.n]]&&w[v][(u.t)%L[v]])continue;
if(L[v]!=0&&mem[v][(u.t+1)%L[v]])continue;
mem[v][(u.t+1)%(L[v]==0?125:L[v])] = 1;
q.push({v,(u.t+1)%(L[v]==0?125:L[v]),u.a+1});
}
if(w[u.n][(u.t+1)%(L[u.n]==0?125:L[u.n])]==0 && mem[u.n][(u.t+1)%(L[u.n]==0?125:L[u.n])]==0){
q.push({u.n,(u.t+1)%(L[u.n]==0?125:L[u.n]),u.a+1});
mem[u.n][(u.t+1)%(L[u.n]==0?125:L[u.n])] = 1;
}
}
cout << (ans==-1?"impossible":to_string(ans)) << " \n";
}
signed main(){
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4988 KB |
Output is correct |
2 |
Runtime error |
2114 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
5036 KB |
Output is correct |
2 |
Runtime error |
2097 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
5036 KB |
Output is correct |
2 |
Runtime error |
2097 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
5036 KB |
Output is correct |
2 |
Runtime error |
2097 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4988 KB |
Output is correct |
2 |
Runtime error |
2114 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4988 KB |
Output is correct |
2 |
Runtime error |
2114 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4988 KB |
Output is correct |
2 |
Runtime error |
2114 ms |
524292 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |