Jump Man
Once you confirm the aliens come in peace, your team finally relaxes. Communication becomes smoother, you apologise for trying to blow up their ship, and they apologise for kidnapping you in the first place! In space, you can never be too sure...
Before you part ways, you ask the aliens for help getting home. They hand you a Hyperdrive, their latest (experimental) technology.
To guide you, the aliens provide a stellar map:
- The galaxy consists of
stars, numbered from
to
.
- TRAPPIST-1 is star
(your current location), and our Sun is star
(your destination).
- Each star has a directed connection to exactly one other star.
However, the Hyperdrive comes with limitations:
- At each star, you are allowed to "jump" to another star that is exactly
steps away in the connection graph, where
is chosen from a set of allowed distances for that star.
- You cannot jump more or fewer steps than allowed, only values from the given set may be used.
Is it possible to get home using the Hyperdrive?
Input
The first line consists of a single integer,
, the number of stars in the galaxy.
The next lines consist of two integers
and
, representing that star
and
are connected by one step.
The next line consists of a single integer,
, the number of possible jumps in the galaxy.
The next lines consist of two integers
and
, indicating that you can jump exactly
steps from star
.
Output
If it is possible to get to our sun (star ) from TRAPPIST-1 (star
), output "Yes". Otherwise, output "No".
Clarifications
- The graph is weakly connected.
Example
Input 1
6
1 3
2 3
3 4
4 5
5 6
6 3
6
1 1
1 6
2 3
4 2
4 1
5 3
Output 1
Yes
Starting from star , make
jumps. You will end up on star
.
From star , make
jumps. You will end up on star
.
Welcome home!
Input 2
3
1 2
2 3
3 1
2
1 1
2 2
Output 2
No
Input 3
5
1 5
5 2
2 3
3 1
4 5
4
1 2
1 3
2 8
3 3
Output 3
No
Comments