编程介的小学生 2019-06-16 17:30 采纳率: 20.5%
浏览 275

二叉树的涂色的一个问题,怎么用C语言的程序的编写设计的思想方式的过程来实现的呢?

Problem Description
Alice and Bob are playing games again! This time, they invent a new game called "Color the Tree". They draw a tree with N nodes and, certainly, (N-1) edges connecting them to assure a path between each pair of the nodes. But the tree they play with is a little special - each edge is assigned to a color and a specific value. Initially, the value of each edge is settled while the colors are all white.
When the game starts, Alice and Bob each choose a node as her/his starting node. In each round, Alice firstly makes a move from her current node to another through an edge with a color of white or red, and if the edge is white, she colors it to red. After that, Bob makes a similar move through a white or blue edge from his current node, and if the edge is white, he colors it to blue. The game keeps going on until all the edges are colored to either red or blue.
Alice's goal is to maximize the sum of values of the red edges, and Bob, wants to maximize that of the blue edges. Given the starting node of them, figure out the maximum sum that Alice is able to obtain if both of them take the best strategy in each round.

Input
The first line of the input contains a single integer T, indicating there are T test cases.
In each case, the first line contains two integers N and M, which denotes the number of nodes and queries.
Each of the following (N-1) lines contains a triple of integers (a,b,c), indicating there is an edge connecting node a and node b with a value of c.
The following M lines describe the queries. Each of the M lines consists of two integers A and B, indicating the starting node of Alice and Bob, respectively.
(2≤N≤100000,1≤M≤100000,1≤a,b,A,B≤N,1≤c≤1000)

Output
For each query, output the maximum sum that Alice can obtain, one per line.

Sample Input
2
2 1
1 2 3
1 2
3 2
1 2 3
1 3 1
2 3
1 3

Sample Output
3
3
4

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题