LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Cycle Check

EasyProblem #67
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

You do not need a separate cycle search. Kahn's algorithm already tells you.

In the future there will be many worlds, and inside each one people will be able to teleport from planet to planet. A teleporter is one-way: a link from planet a to planet b lets you travel from a to b, but not back.

For each world, decide whether there is any planet you could leave and then return to by taking a series of teleports.

Input

First line of input will be a single integer t - the number of worlds.
The first line of each world contains two integers v and e - the number of planets and the number of teleporters.
Each of the next e lines contains two integers a and b - a teleporter leading from planet a to planet b.

Planets are numbered 0 to v−1.

Output

For every world print a single line: yes if some planet can be left and returned to, and no otherwise.

Example

Input
2
5 5
0 1
2 1
2 3
3 4
4 2
5 5
0 1
2 1
2 3
3 4
4 0
Output
yes
no

In the first world you can leave planet 2 and come back to it by teleporting 2→3→4→2. The second world uses almost the same links, but the last one leads to planet 0 instead of planet 2 - and planet 0 has no teleporter arriving at it, so nothing can ever get back.

Constraints

1≤t≤20
2≤v≤5000
1≤e≤2⋅104
0≤a,b≤v−1
The sum of v over all worlds does not exceed 5⋅104, and the sum of e does not exceed 2⋅105


This problem was adapted, with permission, from Provera ciklusa, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit