This page is still under construction.

Parts of this page are still being built. What you see may change.

Pontoon Bridge

Time limit1sMemory limit256 MB

Summary
Find the smallest connected set of water squares that touches both banks of a river given as water intervals per row.
Level

Medium7 of 10

Topics
Shortest path, Dynamic programming
Solved
No attempts yet

Problem

Some years ago the Hooligan tribe went to war. Their army moves machine guns and tanks, so crossing the many rivers of the region is the hardest part of every march. Hooligan cartographers redraw the land so that every boundary between land and water runs along the sides of the unit squares of a square grid. On such a map every river flows straight down, and every coordinate of the left bank is smaller than every coordinate of the right bank.

The inventor Postolomlatos builds mobile bridges out of pontoons. One pontoon covers exactly one unit square of water. Two pontoons hold together only when they share a whole side, and a pontoon reaches a bank only when it shares a whole side with a square of land. Chief Mlask the Great wants every river crossed with as few pontoons as possible.

The map of one river is KK unit strips high. The strips are numbered 11 to KK from top to bottom. In strip ii the water covers the horizontal coordinates from AiA_i to BiB_i, so the water squares of that strip are (i,Ai),(i,Ai+1),…,(i,Bi−1)(i, A_i), (i, A_i + 1), \dots, (i, B_i - 1), where (i,x)(i, x) is the square of strip ii between the vertical lines xx and x+1x + 1. In strip ii every square with coordinate smaller than AiA_i belongs to the left bank, and every square with coordinate at least BiB_i belongs to the right bank. Nothing exists above strip 11 or below strip KK.

A set SS of water squares is a bridge when all three conditions hold.

  • Any two squares of SS are joined by a chain of squares of SS in which consecutive squares share a whole side.
  • Some square of SS shares a whole side with a square of the left bank.
  • Some square of SS shares a whole side with a square of the right bank.

The square that touches the left bank and the square that touches the right bank may lie in different strips. Compute the smallest number of squares a bridge can have.

Input

The first line contains the number of test cases NN. The first line of each test case contains one positive integer K≤10 000 000K \le 10\,000\,000, the height of the map. Each of the next KK lines describes one horizontal unit strip, from top to bottom, and contains two space separated integers AiA_i and BiB_i: the left and the right coordinate of the river bank in that strip. Always 0≤Ai<Bi≤1 000 0000 \le A_i < B_i \le 1\,000\,000, and every AiA_i is smaller than every BjB_j.

Output

For each test case print exactly one line:

K prechodu reky je treba X pontonu.

Replace XX with the smallest number of pontoons that build a bridge from one bank of the river to the other. Print the sentence exactly as shown, in Czech and without diacritics. The K at the start of the line is the Czech preposition, not the height of the map.

Examples2

  1. Example 1

    Input
    2
    8
    2 8
    3 9
    4 9
    4 8
    2 7
    1 5
    1 6
    0 5
    5
    1 7
    2 7
    4 8
    5 6
    0 6
    
    Expected output
    K prechodu reky je treba 3 pontonu.
    K prechodu reky je treba 1 pontonu.
    
  2. Example 2

    Input
    1
    3
    0 3
    1 4
    2 5
    
    Expected output
    K prechodu reky je treba 3 pontonu.