Tokens
Time limit2sMemory limit1024 MB
Given a directed graph and two token placements of equal size, decide whether tokens can move from the red set to the blue set and back to the red set.
- Level
Hard9 of 10
- Topics
- Graph, DFS, Topological sort
- Solved
- No attempts yet
Problem
Dong-hun gave his friend Chan-kyung a directed graph with vertices and one-way edges. Along with the graph came two cards, and Chan-kyung realized that Dong-hun had prepared a mission to trick him. The mission is as follows.
- One of the two cards is red and the other is blue. Each card lists vertex numbers from 1 to . The numbers on each card are distinct, but one vertex number may appear on both cards. The state where a token sits on every vertex listed on the red card is the 'first state'. The state where a token sits on every vertex listed on the blue card is the 'second state'.
- Place tokens on the graph to reach the first state.
- Repeatedly move tokens to reach the second state. If there is an edge from vertex to vertex , vertex has a token, and vertex has no token, the token on can be moved to .
- Repeatedly move tokens to reach the first state again.
Chan-kyung knows Dong-hun's habits, so he suspects the mission may be impossible from the start. If it is impossible, he will leave and do something else. Help Chan-kyung!
Input
The first line contains the number of vertices and the number of one-way edges .
The next lines each contain two integers and , meaning there is a one-way edge from vertex to vertex . The input has no duplicate edges and no self-loops.
The -th line contains the number of vertices listed on each card.
The -th line contains distinct positive integers , the vertices listed on the red card.
The -th line contains distinct positive integers , the vertices listed on the blue card.
All values on each line are separated by spaces.
Output
Print YES if Chan-kyung can complete the mission, and NO if he cannot.