Muchii Permutate

Time limit: 1.5s Memory limit: 512MB Input: Output:

You are given a tree with NN nodes (numbered from 11 to NN), and we consider its edges numbered from 11 to N1N - 1 (in the order in which they are given in the input). A tree is a connected, acyclic, undirected graph.

Let PP be a permutation of the numbers from 11 to N1N - 1. For this given permutation PP, we assign to each edge of index ii a weight equal to P[i]P[i]. Then, consider an algorithm in which we initially have no edges in the tree, and we start adding them one by one, in the order of their numbering (essentially in the order in which they are given in the input, from 11 to N1N - 1 — the order of addition does not depend on the weights).

For the permutation PP to be considered valid, it is required that at every step of the algorithm, any maximal connected component (with at least 22 nodes) formed so far has only edges with consecutive weights — here the components are considered individually (and only the maximal ones), so all of them must satisfy this condition, at every step of the algorithm.

Requirement

Given the number NN, representing the number of nodes in the tree, as well as the N1N - 1 edges of the tree (in the order given in the statement), determine the minimum number of inversions of a valid permutation, as well as the number of valid permutations that achieve this minimum (modulo 109+710^9 + 7).

Implementation Details

You must implement a single function:

std::pair<long long, int> solve(int N, std::vector<int> U, std::vector<int> V);

which receives as parameters:

  • NN, the number of nodes in the tree
  • The sequences UU and VV, where the pair U[i],V[i]U[i], V[i] represents the endpoints of edge i+1i + 1 in the tree (according to the numbering given in the statement), for ii from 00 to N2N - 2.

The solve function will be called exactly once.

Constraints

  • 2N500 0002 \leq N \leq 500 \ 000
  • 1Ui,ViN1 \leq U_i, V_i \leq N, for 0iN20 \leq i \leq N - 2
  • Note: Only the number of valid permutations with a minimum number of inversions is computed modulo 109+710^9 + 7. The minimum number of inversions itself is computed as an exact value.
# Score Constraints
1 2 N200 000N \leq 200 \ 000 and the given tree has a star shape (there is a node of degree N1N - 1)
2 4 N10N \leq 10
3 11 N500N \leq 500
4 13 N5 000N \leq 5 \ 000
5 21 N200 000N \leq 200 \ 000 and the tree is a chain (all nodes have degree at most 22)
6 22 N200 000N \leq 200 \ 000
7 27 No additional restrictions.

Example 1

input

7
3 7
2 5
2 6
1 3
1 2
2 4

stdout

2 2

Explanation

For the first example, the two optimal valid permutations that satisfy the constraints and achieve the minimum of 22 inversions are:

  • P=(1,3,4,2,5,6)P = (1, 3, 4, 2, 5, 6)
  • P=(3,1,2,4,5,6)P = (3, 1, 2, 4, 5, 6)

Example 2

input

20
16 17
4 5
8 9
11 12
18 19
1 2
14 15
7 8
19 20
6 7
15 16
3 4
12 13
9 10
2 3
17 18
5 6
10 11
13 14

output

48 8

Explanation

For the second example, the minimum number of inversions of a valid permutation is 4848, and there are 88 valid permutations with this minimum number of inversions.

Log in or sign up to be able to send submissions!