//最短経路をBFSで求めて最短経路上の頂点からの距離がK_i以上の頂点J_iが存在しないかどうかで判定(嘘解法) #include #include #include #include using namespace std; using namespace atcoder; using ll = long long; //#define endl "\n"; ll N, M, L; vector> G; int main(){ cin >> N >> M; G.resize(N + 1); for(int i = 0; i < M; i++){ ll a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } cin >> L; vector chk(N + 1, -1); for(int i = 0; i < L; i++){ ll j, k; cin >> j >> k; chk[j] = k; } queue que; while(1){ vector dist(N + 1, -1); if(chk[1] >= 0){ cout << "No" << endl; return 0; } dist[1] = 0; que.push(1); while(!que.empty()){ int pos = que.front(); que.pop(); for(auto to: G[pos]){ if(dist[to] == -1 && chk[to] == -1){ dist[to] = dist[pos] + 1; que.push(to); } } } if(dist[N] == -1){ cout << "No" << endl; return 0; } ll now = N; bool isOK = true; while(1){ vector visited(N + 1, -1); visited[now] = 0; que.push(now); while(!que.empty()){ int pos = que.front(); que.pop(); if(visited[pos] <= chk[pos]){ chk[now] = 0; isOK = false; } for(auto to: G[pos]){ if(visited[to] == -1){ visited[to] = visited[pos] + 1; que.push(to); } } } if(now == 1) break; for(auto to: G[now]){ if(dist[to] == dist[now] - 1){ now = to; break; } } } if(!isOK) continue; cout << "Yes" << endl; cout << dist[N] << endl; break; } return 0; }