Through quantum superposition, you are in two universes at the same time. Each universe can be represented as a directed acyclic graph. You begin at the start vertices in each universe and your goal is to reach the end vertices. Now, you are wondering, can you reach the end vertices in both universes where the sum of steps made in each universe is exactly equal to a given number?
Input
The first line contains 1≤N1,N2≤10001≤N1,N2≤1000, the number of vertices in each universe, and 0≤M1,M2≤20000≤M1,M2≤2000, the number of edges in each universe. In both universes, vertex 11 is the start vertex. Vertices N1N1 and N2N2 are the end vertices in each respective universe.
Each of the next M1M1 lines contains two numbers 1≤u,v≤N11≤u,v≤N1, indicating a directed edge from vertex uu to vertex vv in universe 1.
Each of the next M2M2 lines contains two numbers 1≤u,v≤N21≤u,v≤N2, indicating a directed edge from vertex uu to vertex vv in universe 2.
There will not be duplicate edges. It is guaranteed that the universes are acyclic, and there exists a path from start to end vertex in each universe.
The next line contains 1≤Q≤20001≤Q≤2000, the number of queries. Each query consists of a line with a single integer 0≤q≤20000≤q≤2000 that is the sum of steps made in each universe.
Output
For each query, output “Yes” if it is possible to reach the end vertices in both universes where the sum of steps made in each universe is exactly equal to the given query, or “No” otherwise.
Sample Input 1 | Sample Output 1 |
---|---|
3 2 3 1 1 2 1 3 2 3 1 2 5 0 1 2 3 4 | No No Yes Yes No |
//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/ //博客园
//https://blog.csdn.net/qq_44134712 //csdn
using namespace std;
const int N=5e4+10;
typedef long long ll;
const int mod=1e9+7;
vector<int>n[2010];
vector<int>m[2010];
int cnt1,cnt2;
int disv1[2010];
int disv2[2010];
map<int,int>nm;
int flag[2010][2010];
int a,b,c,d;
void dfs1(int pos,int num){
if(pos==a){
disv1[++cnt1]=num;
}
flag[pos][num]==1;
int len=n[pos].size();
for(int i=0;i<len;i++){
int g=n[pos][i];
if(flag[g][num+1]==0){
flag[g][num+1]=1;
dfs1(g,num+1);
}
}
}
void dfs2(int pos,int num){
if(pos==b){
disv2[++cnt2]=num;
}
flag[pos][num]==1;
int len=m[pos].size();
for(int i=0;i<len;i++){
int g=m[pos][i];
if(flag[g][num+1]==0){
flag[g][num+1]=1;
dfs2(g,num+1);
}
}
}
int main(){
ios::sync_with_stdio(false);
int x,y;
cin >> a >> b >> c >> d;
for(int i=1;i<=c;i++){
cin >> x >> y;
n[x].push_back(y);
}
for(int i=1;i<=d;i++){
cin >> x >> y;
m[x].push_back(y);
}
memset(flag,0,sizeof(flag));
dfs1(1,0);
memset(flag,0,sizeof(flag));
dfs2(1,0);
for(int i=1;i<=cnt1;i++){
for(int j=1;j<=cnt2;j++){
nm[disv1[i]+disv2[j]]=1;
}
}
int k,h;
cin >> k;
while(k--){
cin >> h;
if(nm[h]==1){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
}
return 0;
}