H - Quantum Superposition Kattis - quantumsuperposition (dfs)

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 1Sample 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值