Under construction

Still being built. Things may change

New Megacity

Interview

Time limit2.5sMemory limit2048 MB

Summary
Given n cities and m weighted roads, label each road 1 if every minimum spanning tree uses it, 2 if some do but not all, and 3 if none do.
Level

Medium6 of 10

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

Problem

You are part of a large project to design a new megacity that connects nn cities. There are mm candidate roads, and each road has a construction cost. All cities must be connected at the minimum total cost, which means they must form a minimum spanning tree (MST). Not all roads matter equally, though. Classify each road into one of three types:

  • Type 1: The road is included in every MST.
  • Type 2: The road is included in at least one MST, but not in every MST.
  • Type 3: The road is not used in any MST.

Input

The first line contains two integers nn and mm, the number of cities and the number of candidate roads. The bounds are 2≤n≤100 0002 \le n \le 100\,000 and n−1≤m≤min⁡(100 000,n(n−1)/2)n - 1 \le m \le \min(100\,000, n(n - 1)/2). Each of the next mm lines contains three integers xx, yy, and zz, meaning that a road connects cities xx and yy and costs zz. Here x≠yx \ne y, 1≤x,y≤n1 \le x, y \le n, and 1≤z≤100 0001 \le z \le 100\,000. No two cities are connected by more than one road. The input always has at least one MST.

Output

Print mm lines. The ii-th line contains a single integer, 1, 2, or 3, which is the type of the ii-th road in input order.

Examples2

  1. Example 1

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

    Input
    4 5
    1 2 1
    1 3 1
    2 3 1
    2 4 1
    3 4 1
    
    Expected output
    2
    2
    2
    2
    2