/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 3e5+100, M = 1e5+10, K = 20;
int n, m, k;
array<int, 2> T[N];
vector<int> g[N];
priority_queue<int> best[N];
void solve(){
cin >> n >> m;
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; ++i) T[i] = {-1, 0};
cin >> k;
for(int i = 1; i <= k; ++i){
int x; cin >> x;
for(int j = 0; j < x; ++j){
int nod; cin >> nod;
T[nod] = {j, x};
}
}
priority_queue<array<int, 2>> q;
q.push({0, 1});
best[1].push(0);
while(!q.empty()){
int v = q.top()[1], D = -q.top()[0];
q.pop();
if(D > best[v].top()) continue;
for(int u: g[v]){
for(int j = 1; j <= K; ++j){
int DD = D + j;
if(T[v][0] > -1 && (DD-1) % T[v][1] == T[v][0]) break;
if(T[v][0] > -1 && T[u][0] > -1 && DD % T[v][1] == T[v][0] && (DD-1) % T[u][1] == T[u][0]){
continue;
}
if(T[u][0] > -1 && DD % T[u][1] == T[u][0]){
continue;
}
if(best[u].size() < K){
best[u].push(DD);
q.push({-DD, u});
}else if(best[u].top() > DD){
best[u].pop();
best[u].push(DD);
q.push({-DD, u});
}
}
}
}
if(best[n].empty()) cout << "impossible";
else{
while(best[n].size() > 1) best[n].pop();
cout << best[n].top();
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:72:15: warning: unused variable 'aa' [-Wunused-variable]
72 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
935 ms |
36384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
893 ms |
36300 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
893 ms |
36300 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
893 ms |
36300 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
935 ms |
36384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
935 ms |
36384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
18264 KB |
Output is correct |
2 |
Incorrect |
935 ms |
36384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |