//全探索BFS+N→1(嘘解法) #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; if(chk[N] >= 0){ cout << "No" << endl; return 0; } vector dist(N + 1, -1); dist[N] = 0; que.push(N); while(!que.empty()){ int pos = que.front(); que.pop(); for(auto to: G[pos]){ if(chk[to] == -1){ queue que2; vector visited(N + 1, -1); bool isOK = true; que2.push(to); visited[to] = 0; while(!que2.empty()){ int pos2 = que2.front(); que2.pop(); if(!isOK) continue; if(visited[pos2] <= chk[pos2]){ isOK = false; continue; } for(auto to2: G[pos2]){ if(visited[to2] == -1){ visited[to2] = visited[pos2] + 1; que2.push(to2); } } } if(!isOK){ chk[to] = 0; continue; } }else{ continue; } if(dist[to] == -1){ dist[to] = dist[pos] + 1; que.push(to); } } } if(dist[1] == -1){ cout << "No" << endl; }else{ cout << "Yes" << endl; cout << dist[1] << endl; } return 0; }