Bay
시간 제한1초메모리 제한2048 MB
n x n 격자 그래프의 신장 트리에서, 비트리 간선으로 만들어지는 사이클이 정확히 S개의 단위 칸을 감쌀 때 그 간선의 개수와 사전순으로 가장 앞선 간선을 구한다.
문제
We have a grid (lattice) graph , where is the number of vertices along both the -axis and the -axis, that is, the number of rows and columns. The vertices of the graph are numbered consecutively from to in row-major ordering; starting from the top-left vertex, we traverse row by row from top to bottom, and within each row from left to right. Figure 1 shows two examples, and with vertex numbers.

Figure 1. Left: Grid graph . Right: .
We are given a spanning tree of . The left in Figure 2 shows a spanning tree of . If we add an edge of that does not belong to (called non-tree edge), then exactly one simple cycle is created. We define the region enclosed by this cycle as a bay. There is a one-to-one correspondence between non-tree edges and bays, that is, each non-tree edge corresponds to exactly one bay. The area of a bay is defined by number of unit cells enclosed by the cycle. The right in Figure 2 shows two bays(colored blue and orange) created by adding two non-tree edges and , respectively. Note that the areas of two bays created by and are and , respectively.

Figure 2. A spanning tree of a grid and two bays created by and .
Given a spanning tree of a grid graph and a positive integer , write a program that finds all non-tree edges that creates bays of area and outputs the first non-tree edge among them in lexicographical order.
입력
Your program is to read from standard input. The input starts with a line containing two integers and where for and . Each of the following lines contains two distinct integers and representing an edge of a spanning tree , where .
출력
Your program is to write to standard output. The first line should contain the number of non-tree edges that create the bays of area . The second line should contain two distinct integers and () representing the first non-tree edge in lexicographical order among those that create the bays of area . The lexicographical order of two edges and is defined such that comes before if and only if or, if then . If there is no non-tree edge that creates the bay of area , then print “0” in the first line and two zeros “0 0” in the second line.
Figure 3 shows two spanning trees of a grid graph with , which are the sample inputs and outputs.

Figure 3. Two spanning trees of for Sample Input 1 and 2.