//多始点BFS(嘘解法) #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; queue> quep; vector chk(N + 1, -1); for(int i = 0; i < L; i++){ ll j, k; cin >> j >> k; chk[j] = k; quep.push({j, chk[j]}); } while(!quep.empty()){ auto [pos, d] = quep.front(); quep.pop(); for(auto to: G[pos]){ if(chk[to] < d - 1){ chk[to] = d - 1; quep.push({to, chk[to]}); } } } queue que; vector dist(N + 1, -1); if(chk[1] != -1){ cout << "No" << endl; }else{ 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; }else{ cout << "Yes" << endl; cout << dist[N] << endl; } } return 0; }