Bad Trees
View as PDF
Submit solution
Points:
100
Time limit:
1.0s
PyPy 3
3.0s
Python 3
3.0s
Memory limit:
500M
Problem type
Andy wants to construct an undirected graph with no cycles, except he doesn't know how. Instead, he tries to randomly add edges between two nodes. Tell Andy whether adding each edge will result in a forest (an undirected graph with no cycles). If so, add the edge to the graph.
The graph begins with no edges.
Input
The first line contains integers and
, the number of nodes in the graph and the number of edges that Andy is trying to add, respectively.
The following lines each contain two integers
and
, stating that Andy wishes to add an undirected edge between
and
.
Output
For each edge Andy tries to add, output a line containing Yes or No, answering whether the graph will remain a forest after adding that edge.
Constraints
Example 1
Input
5 10
3 3
1 1
2 4
3 3
4 4
5 4
3 5
4 3
1 3
2 5
Output
No
No
Yes
No
No
Yes
Yes
No
Yes
No
Example 2
Input
10 20
9 4
3 4
10 4
4 3
9 4
10 3
10 5
5 3
9 10
6 10
6 10
3 4
4 8
7 9
1 2
8 2
3 6
10 6
8 4
5 9
Output
Yes
Yes
Yes
No
No
No
Yes
No
No
Yes
No
No
Yes
Yes
Yes
Yes
No
No
No
No
Comments