Under construction

Still being built. Things may change

Octopus's Garden

Time limit5sMemory limit1024 MB

Summary
Given a triangulated convex polygon with interior points, list all triangles so that every prefix forms a simply connected set of triangles, starting from a given triangle.
Level

Hard9 of 10

Topics
Graph, DFS, BFS, Implementation
Solved
No attempts yet

Problem

Octopuses are located on the surface of the planar seabed in the most beautiful manner. Octopuses like their location if it specifies a partition of the seabed plane into triangles such that the octopuses' heads are in triangle vertices, and triangle edges are formed by their outstretched tentacles. They call such partition a triangulation. Let us formalize this notion with the help of graph theory.

A planar graph is a graph that can be drawn on a plane in such a way that no edges cross each other.

A face of a graph drawn without crossings is a simple cycle which surrounds such a region that there are no edges or vertices inside the region.

An outer face of a graph drawn without crossings is the region which has infinite area.

A triangulation of a set of points on the plane is a connected planar graph drawn in such a way that its vertices are in these points, and every face except the outer face is a non-degenerate triangle formed by exactly three edges of the graph.

Two triangles are connected by side if they have a common side.

A triangle and the outer face are connected by side if one of the triangle's sides touches the outer face.

Let us denote the set of faces of the triangulation by SS.

A sequence of elements of SS is called a path if every two consecutive members of it are connected by side.

A subset of SS is said to be connected by side if for any two elements of this subset, there exists a path such that all its elements belong to this subset.

We are given a triangulation SS.

Consider a subset of SS that does not contain the outer face. We will say such a subset is simply connected if it satisfies the following conditions:

  • the subset is connected by side;
  • the complement of the subset in SS also is connected by side.

Octopuses like simply connected sets because they plan to take contour integrals around their border.

One of the triangles in the triangulation is fixed as the starting triangle.

Help the octopuses to find a sequence of all triangles in the triangulation which starts with the starting triangle, and for any 1≤i≤K1 \le i \le K where KK is the number of triangles, the set of triangles T1T_{1}, T2T_{2}, …\ldots, TiT_{i} is simply connected.

Input

The first line of input contains two integers NN (3≤N≤100 0003 \le N \le 100\,000) and MM: the number of octopuses and the number of edges formed by their outstretched tentacles. The second line contains three integers AA, BB and CC: the numbers of octopuses that occupy the vertices of the starting triangle.

Each of the following NN lines contains two integers XX and YY: coordinates of points on the seabed plane where octopuses are located. (−10 000≤X,Y≤10 000-10\,000 \le X, Y \le 10\,000). All these points are distinct.

Each of the following MM lines contains two integers UU and VV: the numbers of two octopuses forming an edge by their outstretched tentacles.

Octopuses are numbered from one in the order they are described. An octopus may have any number of tentacles, not only eight as in nature.

It is known that the subset of SS consisting of all its elements except the outer face is connected by side. The union of all elements of this subset forms a convex polygon.

Output

On the first line of output, print an integer KK: the number of triangles formed by octopuses. On each of following (K−1)(K - 1) lines, print three integers Pi(1)P^{(1)}_{i}, Pi(2)P^{(2)}_{i}, Pi(3)P^{(3)}_{i}: the numbers of three octopuses which form triangle TiT_{i} (2≤i≤K2 \le i \le K). For every ii such that 1≤i≤K1 \le i \le K, the set of triangles T1T_{1}, T2T_{2}, …\ldots, TiT_{i} must be simply connected. If there is more than one possible solution, output any one of them.

Hint

In the first example:

  • the triangle sequence such that the first triangle is formed by octopuses 1, 2, 3 and the second one by 2, 4, 5 is incorrect because one of conditions is not satisfied: this triangles are not connected by side;
  • the triangle sequence such that the first triangle is formed by octopuses 1, 2, 3 and the second one by 2, 3, 5 is correct;
  • the triangle sequence such that the first triangle is formed by octopuses 1, 2, 3, the second one by 3, 5, 6 and the third one by 2, 4, 5 is incorrect because both conditions are not satisfied: the set formed by this sequence is not connected by side, and the complement set is also not connected by side.

The second example contains only one triangle, so output only one integer KK.

Examples2

  1. Example 1

    Input
    6 9
    1 2 3
    2 0
    1 1
    3 1
    0 2
    2 2
    4 2
    1 2
    1 3
    2 3
    2 4
    2 5
    3 5
    3 6
    4 5
    5 6
    
    Expected output
    4
    2 3 5
    2 4 5
    3 5 6
    
  2. Example 2

    Input
    3 3
    1 2 3
    1 1
    1 2
    2 1
    1 2
    2 3
    3 1
    
    Expected output
    1