Islands and Bridges
Time limit1sMemory limit128 MB
Find the maximum score of a Hamilton path on a graph where the score adds vertex values, edge products, and triangle products, and count how many paths achieve it.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Graph, Combinatorics
- Solved
- No attempts yet
Problem
You are given a map of islands connected by bridges. A Hamilton path is a path that travels along the bridges and visits every island exactly once. Each island also carries a positive integer value. Among all Hamilton paths we look for the one that maximizes the score defined below; we call it the best triangular Hamilton path.
Suppose there are islands. For a Hamilton path , let be the value of island . The score of the path is the sum of three parts:
- First part: the sum of over every island on the path.
- Second part: for every consecutive pair on the path, add the product .
- Third part: for every three consecutive islands that form a triangle on the map (that is, there is also a bridge directly between and ), add the product .
Your first task is to report the maximum possible score. Because several different Hamilton paths may reach this maximum, your second task is to report how many best triangular Hamilton paths exist.
Input
The first line contains an integer (), the number of test cases. Each test case is given as follows:
- A line with two integers and : the number of islands and the number of bridges. There are at most islands.
- A line with positive integers; the -th of them is the value of island . Each value is at most .
- lines, each of the form
x y, meaning there is a two-way bridge between island and island . Islands are numbered from to .
Output
For each test case, print one line with two numbers separated by a space: first the maximum score of a best triangular Hamilton path, then the number of distinct best triangular Hamilton paths. If the map has no Hamilton path at all, print 0 0.
A path written in reverse order is considered the same path.