GUVI
Back

GUVI FORUM CONTEST WEEK 2 - 14 NOV 2021

Created 2 years ago
72 Views
0 Comments
TEAMCS
@TEAMCS
TEAMCS
@TEAMCSProfile is locked. Login

You are given a tree consisting of n nodes numbered from 1 to n. The weights of edges of the tree can be any binary integer satisfying the following Q conditions.

Each condition is of the form u, v, x where u, v are nodes of the tree and x is a binary number.

For satisfying this condition, the sum of the weight of all the edges present in the path from node u to v of the tree should be even if x = 0, odd otherwise.

Now, you have to find out a number of ways of assigning 0/1 (binary) weights to the edges of the tree satisfying the above conditions.

As the answer could be quite large, print your answer modulo 10^9 + 7.

Input

The first line of input contains a single integer T denoting the number of test cases. For each test case:

First-line contains two space-separated integers n, Q.

Each of the next n - 1 lines will contain two space-separated integers u, v denoting that there is an edge between vertex u and v in the tree.

Each of the next Q lines will contain three space-separated integers u, v, x denoting a condition as stated in the problem.

Output

For each test case, output a single integer corresponding to the answer of the problem.

Constraints

1 ≤ u, v ≤ n

0 ≤ x ≤ 1

Input:

3

3 2

1 2

1 3

1 2 0

1 3 0

3 0

1 2

2 3

3 1

1 2

2 3

1 2 1

Output:

1

4

2

Explanation:

In the first example, You can only set the weight of each edge equal to 0 for satisfying the given condition. So, there is exactly one way of doing this. Hence the answer is 1.

In the second example, There are two edges and there is no condition on the edges. So, you can assign them in 4 ways.

In the third example, You have to assign the weight of the edge between node 1 and 2 to 1. You can assign the remaining edge from 2 to 3 either 0 or 1. So, the answer is 2.

Input:

1

3 3

1 3

2 4

1 1 3

2 2 4

3 3 5

Output:

0

Input:

1

3 3

1 2

2 3

2 1 1

3 2 0

2 3 0

Output:

1

Input:

2

3 2

1 2

1 3

2 3 0

2 1 0

2 2

1 2

1 2 1

2 1 1

Output:

1

1

Input:

1

2 1

2 3

2 1 0

Output:

1

DEBUG THE CODE:

mod = 10**9 + 7

def find(n):

        if parent[n] < 0:

            return n

        else:

            pt = find(parent[n])

            xor[n] ^= xor[parent[n]] 

            parent[n] = pt

            return pt

for _ in range(int(input())):

    n, q = map(int, input().split())

    for i in range(n-1): 

        input()

    parent = [-1]*n

    xor = [0]*n  

    f = n-1

    for qq in range(q):

        i, j, w = map(int, input().split())

        if f == -1: continue

        i += 1

        j += 1

        fi = find(i)

        fj = find(j)

        if fi == fj:

            if xor[i] ^ xor[j] ^ w: 

                f = 1

        else:

            f += 1

            if parent[fi] == parent[fj]:

                parent[fi] += 1

            if parent[fi] > parent[fj]:

                parent[fi] = fj

Kindly provide the answer using the below link:

https://forms.gle/EykTLeEzF34zC2eY6

Comments
Please login to comment.
 
Powered by habitate.io Habitate