This page is still under construction.

Parts of this page are still being built. What you see may change.

Islands and Bridges

Time limit1sMemory limit128 MB

Summary
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 nn islands. For a Hamilton path C1C2…CnC_1 C_2 \ldots C_n, let ViV_i be the value of island CiC_i. The score of the path is the sum of three parts:

  • First part: the sum of ViV_i over every island on the path.
  • Second part: for every consecutive pair CiCi+1C_i C_{i+1} on the path, add the product Vi⋅Vi+1V_i \cdot V_{i+1}.
  • Third part: for every three consecutive islands CiCi+1Ci+2C_i C_{i+1} C_{i+2} that form a triangle on the map (that is, there is also a bridge directly between CiC_i and Ci+2C_{i+2}), add the product Vi⋅Vi+1⋅Vi+2V_i \cdot V_{i+1} \cdot V_{i+2}.

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 qq (q≤20q \le 20), the number of test cases. Each test case is given as follows:

  • A line with two integers nn and mm: the number of islands and the number of bridges. There are at most 1313 islands.
  • A line with nn positive integers; the ii-th of them is the value ViV_i of island ii. Each value is at most 100100.
  • mm lines, each of the form x y, meaning there is a two-way bridge between island xx and island yy. Islands are numbered from 11 to nn.

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.

Examples2

  1. Example 1

    Input
    2
    3 3
    2 2 2
    1 2
    2 3
    3 1
    4 6
    1 2 3 4
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    
    Expected output
    22 3
    69 1
    
  2. Example 2

    Input
    1
    1 0
    7
    
    Expected output
    7 1