GRAPHCUT - Graph Cut

no tags 

Given a graph G, and a subset of its vertices X. The associated cut of X is the set of edges associated to X is the subset of all edges in G such that exactly one of the two vertices it joins belongs to X. In thinks, you will be given a graph and a subset of its edges, and you will have to determine whether there exists a subset of the vertices of the graph for which the given subset of the edges is its associated cut.

Input

The first line contains an integer T, the number of test cases (1 ≤ T ≤ 40). Each test case, consists of a line which contains three integers N (2 ≤ N ≤ 500), E (1 ≤ E ≤ 104), K (1 ≤ K ≤ E), the number of vertices in the graph, and the number of edges in the subset for which we want to know whether it is an associated cut or not. Then, E lines follow, each of them contains two integers u, v (1 ≤ u, v ≤ N) which are the vertices joined by the edge, the first K of these E lines represent the asked subset.

Output

Output T lines, one for each test case. If the asked subset is an associated cut, then print “YES”, otherwise print “NO”.

Example

Input:
2

3 3 1
1 2
2 3
1 3

12 17 6
3 4
5 6
10 11
1 5
6 10
4 8
1 2
2 3
6 7
7 8
9 10
11 12
5 9
2 6
3 7
7 11
8 12 Output: NO
YES

hide comments
slothsphereoj: 2024-02-29 04:56:22

Not a standard bipartite graph problem. BFS is fast enough.

devil: 2015-01-16 15:56:47

finally accepted, learn't something new..!!!

Last edit: 2015-01-16 21:11:44
newbie: 2014-01-30 07:56:34

E seems to be greater then 104..its around 10000..to be on safe side

যোবায়ের: 2013-03-21 07:51:43

E <= 104 or 10^4 ?

~ adieus ~: 2012-02-16 04:55:13

can you please let me know , why is my latest submission giving me a sigsegv ?

Los Desempleados FIIS: 2012-02-10 16:01:23

1 and 2 are in different sets, 3 must be in the same set as one of them but that always gives you two edges in the asocciated cut.

Last edit: 2012-02-10 16:24:36
Unicamp Alfa: 2012-02-10 15:43:18

Could you explain why the answer of the first test case is "No"?


Added by:Paulo Costa
Date:2012-02-07
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:UNI