#P7846. 「dWoi R2」Arcade hall / 街机厅

「dWoi R2」Arcade hall / 街机厅

Description

The country of Yaoyao-Yaoyao-Ba-Yiling has nn cities. They are connected by a magical communication tool — the “Senpai Talisman”. The Senpai Talisman of the ii-th city is engraved with a positive integer wiw_i. There are n1n-1 roads among these nn cities. The jj-th road connects city uju_j and city vjv_j, and has an attribute tjt_j. This road is denoted as (uj,vj,tj)(u_j,v_j,t_j), where tj{0,1,2}t_j \in \{0,1,2\}, meaning:

  • When tj=0t_j=0, city uju_j and city vjv_j are in an antagonistic relationship.
  • When tj=1t_j=1, city uju_j and city vjv_j are in an equal relationship.
  • When tj=2t_j=2, city uju_j and city vjv_j are in a friendly relationship.

Each road is bidirectional, and it is guaranteed that any two cities u,vu,v are mutually reachable.

Recently, Mars has suffered from the MARS-514 virus outbreak, so the construction of the Senpai Talisman system must be accelerated. We define:

  • wi[1,R]w_i \in [1,R], and wiw_i is a positive integer.
  • For a road (p,q,r)(p,q,r), the following requirements must be satisfied:
    • When r=0r=0, i.e., city pp and city qq are antagonistic, it must hold that wpwqw_p \ne w_q.
    • When r=2r=2, i.e., city pp and city qq are friendly, it must hold that wp=wqw_p=w_q.
    • When r=1r=1, i.e., city pp and city qq are equal, there is no requirement on the relationship between wpw_p and wqw_q.

After assigning values to wiw_i, treat wiw_i as a sequence. Determine how many essentially different sequences wiw_i can be formed.

In addition, the ruler of Yaoyao-Yaoyao-Ba-Yiling, Haoer Jiejie, found that the larger wiw_i is, the more ink is needed, and the harder it is to build. Therefore, Haoer Jiejie wants to know the minimum possible value of the sum of wiw_i.

Note that, in this problem, sequences AiA_i and BiB_i are essentially the same if and only if for all ii, Ai=BiA_i=B_i.

“Essentially different” means two sequences that are not essentially the same.


Brief statement:

  • There is a tree with nn nodes. Node ii has a node weight wiw_i, and edge jj has an edge weight tjt_j.
  • For each edge (uj,vj,tj)(u_j,v_j,t_j), the endpoint node weights must satisfy:
    • tj=0t_j=0, wujwvjw_{u_j} \ne w_{v_j}.
    • tj=1t_j=1, no requirement.
    • tj=2t_j=2, wuj=wvjw_{u_j}=w_{v_j}.
    • For any node weight, wi[1,R]w_i \in [1,R].
  • When wiw_i is treated as a sequence, find (1) the number of essentially different sequences wiw_i, and (2) the minimum possible sum of wiw_i.

Input Format

The first line contains two integers n,Rn,R, representing the number of cities and the value range.

The next n1n-1 lines each contain three integers uj,vj,tju_j,v_j,t_j, describing a road.

Output Format

Output one line with two integers: the number of essentially different sequences wiw_i that satisfy the requirements, and the minimum possible sum of wiw_i.

If there is no essentially different sequence (i.e., the answer to the first question is 00), then output 00 for the second question as well.

Only the first answer is taken modulo 109+710^9+7.

3 3
1 2 0
1 3 0
12 4
9 3
1 2 0
1 3 1
1 4 1
2 5 2
2 6 1
6 7 0
4 8 2
4 9 0
648 12

Hint

Explanation for Sample 1

The roads in the sample are distributed as follows:

There are 1212 assignment methods in total:

  1. wi={1,2,2}w_i=\{1,2,2\}.
  2. wi={1,2,3}w_i=\{1,2,3\}.
  3. wi={1,3,2}w_i=\{1,3,2\}.
  4. wi={1,3,3}w_i=\{1,3,3\}.
  5. wi={2,1,1}w_i= \bf \{2,1,1\}, this is the optimal case.
  6. wi={2,1,3}w_i=\{2,1,3\}.
  7. wi={2,3,1}w_i=\{2,3,1\}.
  8. wi={2,3,3}w_i=\{2,3,3\}.
  9. wi={3,1,1}w_i=\{3,1,1\}.
  10. wi={3,1,2}w_i=\{3,1,2\}.
  11. wi={3,2,1}w_i=\{3,2,1\}.
  12. wi={3,2,2}w_i=\{3,2,2\}.

Explanation for Sample 2

The roads in the sample are distributed as follows:

For the second question, one optimal assignment is: wi={2,1,1,1,1,1,2,1,2}w_i=\{2,1,1,1,1,1,2,1,2\}.

Constraints and Notes

This problem uses bundled testdata.

  • Subtask 1 (5 pts): tj=1t_j=1 or tj=2t_j=2.
  • Subtask 2 (5 pts): R=1R=1.
  • Subtask 3 (10 pts): uj=ju_j=j, vj=j+1v_j=j+1.
  • Subtask 4 (20 pts): tj=0t_j=0.
  • Subtask 5 (10 pts): n10n \le 10, R5R \le 5.
  • Subtask 6 (50 pts): no special constraints.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1R1001 \le R \le 100.

For Subtask 1 ~ 5, R40R \le 40.

In the subtask descriptions above, tj=Pt_j=P means that for all j[1,n)j \in [1,n), tj=Pt_j=P.

For Subtask 1, “or” means that part of the test points in Subtask 1 satisfy tj=1t_j=1, and the other part satisfy tj=2t_j=2.

Translated by ChatGPT 5