/*input
6 6
1 2
2 3
3 4
4 5
5 2
5 6
1
4 4 5 2 3
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
//#pragma GCC optimize("O2","unroll-loops","no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x)(int)x.size()
#define SORT_UNIQUE(c)(sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=998244353;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
inline ll mult(ll a,ll b){
if(a<0) a+=MOD;
if(b<0) b+=MOD;
if(a>=MOD) a%=MOD;
if(b>=MOD) b%=MOD;
return(a*b)%MOD;
}
inline ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
const int maxn=15e6+5;
int dist[maxn];
int arr[125];
vector<int> v[100005];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n,m;
cin>>n>>m;
REP(i,m){
int a,b;
cin>>a>>b;
--a;
--b;
v[a].pb(b);
v[b].pb(a);
}
REP(i,n) dist[i]=INF;
int k;
cin>>k;
int l;
REP(i,k){
cin>>l;
REP(j,l) cin>>arr[j],--arr[j];
}
REP(i,n*l) dist[i]=INF;
queue<int> q;
q.push(0);
dist[0]=0;
while(!q.empty()){
int z=q.front();
q.pop();
int i=z%l,j=z/l;
for(auto x:v[j]){
if(arr[(i+1)%l]!=j){
int asd=j*l+(i+1)%l;
if(dist[asd]>dist[z]+1){
dist[asd]=dist[z]+1;
q.push(asd);
}
}
for(auto x:v[j]){
if(x!=arr[(i+1)%l] and !(j==arr[(i+1)%l] and x==arr[i%l])){
int asd=x*l+(i+1)%l;
if(dist[asd]>dist[z]+1){
dist[asd]=dist[z]+1;
q.push(asd);
}
}
}
}
}
int ans=INF;
REP(i,l) MNTO(ans,dist[(n-1)*l+i]);
if(ans==INF) cout<<"impossible";
else cout<<ans;
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:92:18: warning: unused variable 'x' [-Wunused-variable]
92 | for(auto x:v[j]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6080 ms |
3800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6078 ms |
3788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6078 ms |
3788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6078 ms |
3788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6080 ms |
3800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6080 ms |
3800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6080 ms |
3800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |