#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define lli long long int
#define MAX 100000
#define pos first
#define tiempo second
lli n,m,a,b,k,x,y,res;
vector<lli> hijos[MAX+2],pos[127];
lli visitados[127][MAX+2],grupo[127],mod[127];
queue< pair<lli,lli> > cola;
pair<lli,lli> act;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
rep(i,1,n) hijos[i].push_back(i);
rep(i,1,m) {
cin >> a >> b;
hijos[a].push_back(b);
hijos[b].push_back(a);
}
mod[0] = 125;
cin >> k;
rep(i,1,k) {
cin >> mod[i];
rep(j,1,mod[i]) {
cin >> b;
grupo[b] = i;
pos[i].push_back(b);
}
}
cola.push({1,0});
res = -1;
while (!cola.empty()) {
act = cola.front();
cola.pop();
x = grupo[act.pos];
a = act.tiempo % mod[x];
if (visitados[a][act.pos] != 0) continue;
if (act.pos == n) {res = act.tiempo; break;}
visitados[a][act.pos] = 1;
for (auto h : hijos[act.pos]){
y = grupo[h];
b = (act.tiempo+1) % mod[y];
if (!pos[y].empty() && pos[y][b] == h) continue;
if (visitados[b][h] != 0) continue;
if (y == x && y > 0) {
if (pos[y][a] == h && pos[x][b] == act.pos) continue;
}
cola.push({h,act.tiempo+1});
}
}
if (res == -1) cout << "impossible";
else cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6216 KB |
Output is correct |
2 |
Correct |
2797 ms |
106288 KB |
Output is correct |
3 |
Correct |
3185 ms |
98320 KB |
Output is correct |
4 |
Incorrect |
2989 ms |
99456 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6220 KB |
Output is correct |
2 |
Correct |
2836 ms |
106252 KB |
Output is correct |
3 |
Correct |
3158 ms |
98316 KB |
Output is correct |
4 |
Incorrect |
3111 ms |
99476 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6220 KB |
Output is correct |
2 |
Correct |
2836 ms |
106252 KB |
Output is correct |
3 |
Correct |
3158 ms |
98316 KB |
Output is correct |
4 |
Incorrect |
3111 ms |
99476 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6220 KB |
Output is correct |
2 |
Correct |
2836 ms |
106252 KB |
Output is correct |
3 |
Correct |
3158 ms |
98316 KB |
Output is correct |
4 |
Incorrect |
3111 ms |
99476 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6216 KB |
Output is correct |
2 |
Correct |
2797 ms |
106288 KB |
Output is correct |
3 |
Correct |
3185 ms |
98320 KB |
Output is correct |
4 |
Incorrect |
2989 ms |
99456 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6216 KB |
Output is correct |
2 |
Correct |
2797 ms |
106288 KB |
Output is correct |
3 |
Correct |
3185 ms |
98320 KB |
Output is correct |
4 |
Incorrect |
2989 ms |
99456 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6216 KB |
Output is correct |
2 |
Correct |
2797 ms |
106288 KB |
Output is correct |
3 |
Correct |
3185 ms |
98320 KB |
Output is correct |
4 |
Incorrect |
2989 ms |
99456 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |