Under construction

Still being built. Things may change

Road Development

Time limit2sMemory limit256 MB

Summary
Given N cities and Q plans, some carried out, find for each abandoned plan how many unpaved roads on shortest paths in the current graph it would have paved, or -1 if it builds a new road.
Level

Hard9 of 10

Topics
Graph, Union-find, Tree, Dynamic programming
Solved
No attempts yet

Problem

IOI Country is a nation made up of NN cities. The cities are numbered 1,2,…,N1, 2, \ldots, N. Professor JOI became interested in how the road network of IOI Country was developed.

Professor JOI examined materials on the history of IOI Country and learned the following.

  • The cities of IOI Country have been the same from right after its founding to the present. Right after the founding of IOI Country, there was not a single road connecting cities.
  • In year ii after the founding of IOI Country (1≤i≤Q1 \le i \le Q), a plan to improve the traffic conditions between city AiA_i and city BiB_i was made.
  • Some of the planned improvements were carried out as planned, while other plans were abandoned without being carried out.
  • The materials make clear which improvement plans were carried out.
  • Every improvement plan that was carried out was completed within one year.

From another document, Professor JOI learned that a plan to improve the traffic conditions between city AiA_i and city BiB_i works as follows.

  • If city BiB_i cannot be reached from city AiA_i using roads already built at the time the improvement plan is made, a new road connecting city AiA_i and city BiB_i in both directions is built. The newly built road is unpaved.
  • If city BiB_i can be reached from city AiA_i using roads already built at the time the improvement plan is made, then among all such routes, take one that uses the minimum number of roads and pave every unpaved road on it. If there are multiple routes using the minimum number of roads, pave the unpaved roads on all of them in the same way. A road that has already been paved is never paved again.

For further investigation, Professor JOI decided to compute, for each improvement plan that was abandoned without being carried out, how many roads would have been paved by that plan if only that plan had additionally been carried out.

Given the traffic improvement plans of IOI Country and whether each was carried out, write a program that computes, for each improvement plan that was abandoned without being carried out, how many roads would have been paved by that plan if it had been carried out.

Input

Read the following data from standard input.

  • The first line contains two integers NN and QQ separated by a space. This means that IOI Country has NN cities, and Professor JOI is looking at the traffic improvement plans over QQ years from the founding.
  • Of the following QQ lines, the ii-th line (1≤i≤Q1 \le i \le Q) contains three integers TiT_i, AiA_i, BiB_i separated by spaces. The integer TiT_i represents the status of the improvement plan made in year ii after the founding: Ti=1T_i = 1 means the plan was carried out, and Ti=2T_i = 2 means the plan was abandoned without being carried out. The integers AiA_i and BiB_i mean that in year ii after the founding, a plan to improve the traffic conditions between city AiA_i and city BiB_i was made.

Output

For each improvement plan that was abandoned without being carried out, output on a single line the number of roads that would have been paved by that plan if it had been carried out. If carrying out that plan would build a new road, output -1.

Constraints

  • 2≤N≤100 0002 \le N \le 100\,000.
  • 1≤Q≤300 0001 \le Q \le 300\,000.
  • 1≤Ti≤21 \le T_i \le 2 (1≤i≤Q1 \le i \le Q).
  • 1≤Ai≤N1 \le A_i \le N (1≤i≤Q1 \le i \le Q).
  • 1≤Bi≤N1 \le B_i \le N (1≤i≤Q1 \le i \le Q).
  • Ai≠BiA_i \ne B_i (1≤i≤Q1 \le i \le Q).

Examples3

  1. Example 1

    Input
    3 7
    1 1 2
    2 2 1
    2 2 3
    1 2 1
    2 1 2
    1 2 3
    2 1 3
    
    Expected output
    1
    -1
    0
    1
    
  2. Example 2

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

    Input
    7 11
    1 5 1
    1 6 2
    1 1 3
    1 3 5
    1 5 7
    1 4 5
    1 4 1
    2 1 3
    2 3 7
    2 4 3
    2 5 6
    
    Expected output
    0
    1
    0
    -1