This page is still under construction.

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

Power Cables to Sewer Pipes

Time limit1sMemory limit128 MB

Summary
For each graph, remove a maximum-length set of edges while keeping the graph connected, then count the integer partitions of the removed length in meters.
Level

Medium7 of 10

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

Problem

Several villages lie close together in one municipality. A windmill park supplies electricity to all of them, and a network of high-tension power cables connects every village to the park, either directly or indirectly through other villages. To guard against outages when a cable breaks, some redundant cables have been added between villages.

Meanwhile, all of the sewer pipes must be replaced, but there is a severe shortage of materials. The mayor therefore decides to strip out as much redundant cable as possible — accepting the loss of redundancy — and recycle it into sewer pipes, as long as every village stays connected. In other words, the cables left in place must still keep all villages connected to one another, and subject to that, the total length of removed cable is made as large as possible. The total length of redundant cable that can be removed never exceeds 400400 km.

At the metalworks, 11 km of power cable yields 11 m of sewer pipe, and the machines can only make pipes whose length is an integer number of meters. Let the total length of recycled cable be SS meters. The metalworks wants to know the number of ways to cut this SS meters into pipes of positive integer length, where the order of the pipes does not matter. For example, if S=3S = 3, the ways are: one pipe of length 33; one pipe of length 22 and one of length 11; and three pipes of length 11 — three ways in total.

For each test case, determine this number of ways. If the power network has no redundancy at all, so that no cable can be removed, output 00.

Input

The first line contains the number of test cases nn (0<n≤100000 < n \le 10000).

Each test case is then given as follows:

  • A line with one integer mm (2≤m≤1002 \le m \le 100), the number of villages.
  • A line with one integer kk (1≤k≤10001 \le k \le 1000), the number of power cables.
  • kk lines, each with three integers fif_i, tit_i, lil_i separated by spaces, indicating a power cable of length lil_i (1≤li≤4001 \le l_i \le 400) km between village fif_i (1≤fi≤m1 \le f_i \le m) and village tit_i (1≤ti≤m1 \le t_i \le m).

Output

For each test case, output on its own line the number of sewer-pipe combinations that can be made by recycling the maximum length of cable that can be removed without disconnecting any village from the power network. If the network is not redundant and no cable can be removed, output 00.

Examples2

  1. Example 1

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

    Input
    1
    3
    2
    1 2 5
    2 3 7
    
    Expected output
    0