9006: Dragon Seal

内存限制:128 MB 时间限制:12 S
题面:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

An immortal dragon attacked the Byteland. After days of battle, the heroes finally beat the dragon. However, the dragon is immortal such that it can not be killed, so the magicians in Byteland decide to seal the dragon using ancient black magic.

The dragon is scheduled to be sealed under an undirected magic tree with n vertices. In each vertice of the tree, there are two gems. Each gem has its magic value m and power value p. The black magic requires removing exactly one gem from each vertex such that there will be exactly one gem remaining at each vertex. The total power is the sum of the power values of all the gems that remained in the tree.

To escape from sealed, the dragon has a chance to attack. It will choose a target vertex on the tree, then select some vertices among it and its neighbor vertices, and finally start an attack on them. If at least one vertex is attacked, and the bitwise XOR sum among magic values of all the gems in attacked vertices is equal to zero, the tree will be broken, and the dragon escapes. The magicians must never let this happen.

Please write a program to help them determine how to choose gems such that the dragon can never escape, and the total power is maximized.

输入格式

The first line contains a single integer T (1T10), the number of test cases. For each test case:

The first line contains a single integer n (2n60), denoting the number of vertices.

In the next n lines, the i-th line contains four integers mipimi and pi (1mi,mi<2641pi,pi107), describing the two gems in the i-th vertex.

Each of the following n1 lines contains two integers ui and vi (1ui,vinuivi), describing an undirected edge between the ui-th vertex and the vi-th vertex.

It is guarenteed that the input is a tree.

输出格式

For each test case, output a single line containing an integer, denoting the total power to seal the dragon. When it is impossible to seal the dragon, please print ''-1'' instead.

输入样例 复制

2
2
3 1 3 1
3 2 3 2
1 2
3
1 3 2 1
2 2 4 3
1 3 3 2
1 2
2 3

输出样例 复制

-1
8

分类标签