Under construction

Still being built. Things may change

Minimum Spanning Tree

Time limit1sMemory limit128 MB

Summary
Given a weighted graph and several spanning trees written as nested lists, decide for each whether it is a minimum spanning tree.
Level

Medium7 of 10

Topics
Minimum spanning tree, Union-find, Sorting, Implementation
Solved
No attempts yet

Problem

GG is a connected, weighted, undirected graph. A spanning tree TT of GG is a subgraph that (1) is a tree and (2) connects all vertices of GG. The weight of a spanning tree is the sum of the weights of its edges. A minimum spanning tree is a spanning tree whose weight is less than or equal to the weight of every other spanning tree.

Write a program that determines whether a given tree TT is a minimum spanning tree of a given graph GG.

Input

The input consists of one or more test cases. Each test case gives a graph GG and one or more trees to test.

The first line of a test case contains a single integer nn (1<n≤10001 < n \le 1000), the number of vertices of GG. Vertices are numbered from 11 to nn.

The next n−1n-1 lines give the upper triangle of the weighted adjacency matrix:

W(1,2) W(1,3) ... W(1,n-1) W(1,n)
W(2,3) W(2,4) ... W(2,n)
...
W(n-1,n)

where Wi,jW_{i,j} (0≤Wi,j≤10000 \le W_{i,j} \le 1000) is the weight of the edge between vertices ii and jj, and Wi,j=0W_{i,j} = 0 means there is no edge between them.

After the matrix, a line contains a single integer QQ (0<Q≤10000 < Q \le 1000), the number of trees to test on this graph.

Each of the next QQ trees is either a single vertex, given by its number, or is written as

(R T1 T2 ... Tc)

where RR is the root vertex and T1,…,TcT_1, \dots, T_c (0<c≤10000 < c \le 1000) are its subtrees, described recursively in the same way.

The end of the input is a line containing a single 00 in place of nn.

Output

For each tree, print one line of the form

a.b result

where aa is the test case number (starting at 11), bb is the tree number within that test case (starting at 11), and result is YES if the tree is a minimum spanning tree of GG and NO otherwise. Put a single space between bb and result, and leave no trailing spaces.

Examples1

  1. Example 1

    Input
    6 
    2  6 11 0 0 
    0 10  0 0 
    0  0  7 
    3  4 
    5 
    3 
    (6 (3 (1 2)) (4 5)) 
    (3 (1 2) (6 (4 5))) 
    (4 1 2 5 6) 
    5
     6 6  0 6 
     6 0 10 
    10 6 
    10 
    2 
    (1 2 5 (3 4)) 
    (5 4 (3 2 1)) 
    0
    
    Expected output
    1.1 YES
    1.2 YES
    1.3 NO
    2.1 YES
    2.2 YES