Under construction

Still being built. Things may change

Roads

Time limit1sMemory limit128 MB

Summary
Decide whether the graph has a spanning tree with exactly K gravel edges, where each road is gravel or concrete.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree, Union-find, Greedy
Solved
No attempts yet

Problem

The Kingdom of New Asia has NN villages connected by MM roads. Some roads are paved with gravel and others with concrete. Keeping every road toll-free is far too expensive, so the king decides on a new road-maintenance plan.

The king wants the number of free roads to be as small as possible, while still guaranteeing that between any two villages there is exactly one path using only free roads. In other words, the free roads must form a single tree that connects all villages. Moreover, because the king enjoys walking on gravel, he wants exactly KK of the free roads to be gravel roads.

For example, suppose the villages and roads are as in Figure 1a. If the king wants exactly 22 free gravel roads, then making roads (1,2),(2,3),(3,4),(3,5)(1, 2), (2, 3), (3, 4), (3, 5) free (Figure 1b) satisfies the requirements: (1) all villages are connected by free roads, (2) the number of free roads is as small as possible, and (3) exactly two of them, namely (2,3)(2, 3) and (3,4)(3, 4), are gravel. Because such a plan exists, the answer in this case is possible.

Figure 1: (a) An example of the villages and roads of New Asia. Solid lines are concrete roads and dashed lines are gravel roads. (b) A maintenance plan with exactly two free gravel roads; only the free roads are shown.

Given the roads of the kingdom and the number KK of free gravel roads the king wants, write a program that decides whether a maintenance plan meeting the king's requirements exists.

Input

The first line contains three space-separated integers.

  • NN: the number of villages (1≤N≤20 0001 \le N \le 20\,000)
  • MM: the number of roads (1≤M≤100 0001 \le M \le 100\,000)
  • KK: the number of gravel roads the king wants to keep free (0≤K≤N−10 \le K \le N-1)

Each of the next MM lines describes road ii (for ii from 11 to MM); line (i+1)(i+1) describes road ii with three space-separated integers.

  • uiu_i and viv_i: the two villages connected by road ii (villages are numbered 11 to NN)
  • cic_i: the type of road ii; ci=0c_i = 0 means a gravel road and ci=1c_i = 1 means a concrete road.

There is at most one road between any pair of villages.

Output

Print possible on a single line if a maintenance plan meeting the king's requirements exists, and no solution otherwise.

Examples4

  1. Example 1

    Input
    5 7 2
    1 3 0
    4 5 1
    3 2 0
    5 3 1
    4 3 0
    1 2 1
    4 2 1
    
    Expected output
    possible
    
  2. Example 2

    Input
    5 7 0
    1 3 0
    4 5 1
    3 2 0
    5 3 1
    4 3 0
    1 2 1
    4 2 1
    
    Expected output
    possible
    
  3. Example 3

    Input
    5 7 4
    1 3 0
    4 5 1
    3 2 0
    5 3 1
    4 3 0
    1 2 1
    4 2 1
    
    Expected output
    no solution
    
  4. Example 4

    Input
    2 1 0
    1 2 0
    
    Expected output
    no solution