This page is still under construction.

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

Sightseeing Tour

Time limit1sMemory limit128 MB

Summary
Decide whether a mixed graph of one-way and two-way streets has a closed tour that uses every street exactly once starting and ending at the same junction.
Level

Medium7 of 10

Topics
Graph, Union-find, DFS, Implementation
Solved
No attempts yet

Problem

The city council wants to run a sightseeing bus tour through the city so that tourists can see every corner of it. The tour must be planned so that every street is driven along exactly once, and the bus must start and finish at the same junction. Streets are either one-way or two-way, and the tour bus must obey these traffic rules. Determine whether such a sightseeing tour can be constructed.

Input

The first line contains a single positive integer nn, the number of test scenarios.

Each scenario begins with a line containing two positive integers mm and ss (1≤m≤2001 \le m \le 200, 1≤s≤10001 \le s \le 1000): the number of junctions and the number of streets.

Each of the next ss lines describes one street with three integers xix_i, yiy_i, and did_i (1≤xi,yi≤m1 \le x_i, y_i \le m, 0≤di≤10 \le d_i \le 1), where xix_i and yiy_i are the junctions joined by the street. If di=1d_i = 1 the street is one-way (from xix_i to yiy_i); otherwise it is two-way. You may assume there is a junction from which every other junction can be reached.

Output

For each scenario, output a single line containing possible if a sightseeing tour can be constructed, or impossible otherwise.

Examples3

  1. Example 1

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

    Input
    1
    1 1
    1 1 0
    
    Expected output
    possible
    
  3. Example 3

    Input
    1
    2 1
    1 2 0
    
    Expected output
    impossible