This page is still under construction.

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

Awkward Group

Time limit5sMemory limit256 MB

Summary
Count the subsets whose largest inner closeness is smaller than every closeness to the outside.
Level

Medium7 of 10

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

Problem

A community PP is made up of nn people who live in a small town. Two people in PP may be friends, or they may never have met. The closeness of two distinct people xx and yy is a value f(x,y)f(x,y), and f(x,y)=f(y,x)f(x,y) = f(y,x).

Call a subset FF of PP a group, and write ∣F∣|F| for the number of people in it. FF is an awkward group if ∣F∣|F| is neither nn nor 11 and the largest closeness between two distinct members of FF is strictly smaller than every closeness between a member of FF and someone outside FF. That is, FF is awkward when it satisfies both conditions:

1<∣F∣<n1 < |F| < n

max⁡{ f(x,y)∣x≠y, x∈F, y∈F }<min⁡{ f(x′,y′)∣x′∈F, y′∈P−F }\max\{\, f(x,y) \mid x \ne y,\ x \in F,\ y \in F \,\} < \min\{\, f(x',y') \mid x' \in F,\ y' \in P - F \,\}

Given the closeness of every pair of distinct people, write a program that counts the awkward groups of PP.

For example, suppose PP holds three people xx, yy and zz. The groups with 1<∣F∣<31 < |F| < 3 are {x,y}\{x,y\}, {y,z}\{y,z\} and {z,x}\{z,x\}. If the closeness values are f(x,y)=8f(x,y) = 8, f(y,z)=3f(y,z) = 3 and f(z,x)=5f(z,x) = 5, the only awkward group among them is {y,z}\{y,z\}, so the answer is 11.

Input

Read from standard input. The first line holds the number of test cases TT. The first line of each test case holds nn, the number of people in the community PP (1≤n≤10001 \le n \le 1000). The people are numbered 11 to nn. The next n−1n-1 lines hold the closeness values. Line ii holds n−in-i integers hi,i+1,hi,i+2,…,hi,nh_{i,i+1}, h_{i,i+2}, \dots, h_{i,n} separated by single spaces, where hi,jh_{i,j} is f(i,j)f(i,j), the closeness of person ii and person jj (1≤i<j≤n1 \le i < j \le n, 1≤hi,j≤1061 \le h_{i,j} \le 10^6).

Output

Write to standard output. Print exactly one line for each test case. The line holds the number of awkward groups of PP.

Examples2

  1. Example 1

    Input
    3
    3
    8 5
    3
    4
    1 3 5
    6 4
    2
    5
    1 6 5 10
    7 8 9
    2 4
    3
    
    Expected output
    1
    2
    3
    
  2. Example 2

    Input
    2
    1
    2
    5
    
    Expected output
    0
    0