PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000447. Hàng rào kỳ lạ (Weird Fence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hàng rào kỳ lạ (Weird Fence)

Weird Fence

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

For the Sultan's World Weird Fence festival, PP poles stand at distinct points of a coordinate plane. Each pole is red or blue and has one hook.

An unlimited number of chains is available, all with the same integer length LL. A chain has a red end and a blue end; each end must attach to a pole of the corresponding color. A chain can connect two poles if its length is at least their straight-line distance:

d=(Xi−Xj)2+(Yi−Yj)2.d=\sqrt{(X_i-X_j)^2+(Y_i-Y_j)^2}.

A connected pair forms a fence. Each pole may be used in at most one fence. Fences may intersect.

Find the minimum integer length LL permitting at least kk fences, or determine that it is impossible.

Input

The first line contains integers P,kP,k, the pole count and required fence count.

Each of the next PP lines contains integers Xi,YiX_i,Y_i and either red or blue, specifying the location and color of pole ii.

Output

Print the minimum integer chain length, or Impossible if fewer than kk fences can be built for every length.

Subtasks

  • Subtask 1 (20 points): 1≤P≤1001\le P\le100; k=1k=1; −1000≤Xi,Yi≤1000-1000\le X_i,Y_i\le1000 for every 1≤i≤P1\le i\le P; each color is red or blue; all pole locations are distinct.
  • Subtask 2 (30 points): 1≤P≤201\le P\le20; 1≤k≤1001\le k\le100; −1000≤Xi,Yi≤1000-1000\le X_i,Y_i\le1000 for every 1≤i≤P1\le i\le P; each color is red or blue; all pole locations are distinct.
  • Subtask 3 (50 points): 1≤P≤1001\le P\le100; 1≤k≤1001\le k\le100; −1000≤Xi,Yi≤1000-1000\le X_i,Y_i\le1000 for every 1≤i≤P1\le i\le P; each color is red or blue; all pole locations are distinct.

Examples

Input 1

6 2
-3 5 blue
-3 3 red
1 5 blue
2 0 red
4 6 blue
4 -1 red

Output 1

6

Explanation 1

Pair (−3,5)(-3,5) with (−3,3)(-3,3) at distance 2 and (1,5)(1,5) with (2,0)(2,0) at distance 26\sqrt{26}. Both fit length 6. With length at most 5, only the red pole (−3,3)(-3,3) has a blue neighbor; the other red poles are farther than 5 from every blue pole. Its single hook prevents two fences.

Input 2

6 4
-3 5 blue
-3 3 red
1 5 blue
2 0 red
4 6 blue
4 -1 red

Output 2

Impossible

Explanation 2

There are three poles of each color. Four fences require four of each color, so no chain length suffices.

Example 1