Problem10422--USACO 2023 December Contest, Gold Problem 1. Flight Routes

10422: USACO 2023 December Contest, Gold Problem 1. Flight Routes

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

Bessie recently discovered that her favorite pop artist, Elsie Swift, is performing in her new Eras Tour! Unfortunately, tickets are selling out fast, so Bessie is thinking of flying to another city to attend the concert. The Eras tour is happening in $N\ (2≤N≤750)$ cities labeled $1…N$, and for each pair of cities $(i,j)$ with $i<j$ there either exists a single direct flight from $i$ to $j$ or not.
A flight route from city $a$ to city $b\ (a<b)$ is a sequence of $k≥2$ cities $a=c_1<c_2<⋯<c_k=b$ such that for each $1≤i<k$, there is a direct flight from city $c_i$ to city $c_{i+1}$. For every pair of cities $(i,j)$ with $i<j$, you are given the parity of the number of flight routes between them (0 for even, 1 for odd).
While planning her travel itinerary, Bessie got distracted and now wants to know how many pairs of cities have direct flights between them. It can be shown that the answer is uniquely determined.

Input

The first line contains $N$.

Then follow $N−1$ lines. The $i$th line contains $N−i$ integers. The $j$th integer of the $i$th line is equal to the parity of the number of flight routes from $i$ to $i+j$.

Output

Output the number of pairs of cities with direct flights between them.

Sample 1 Input

3
11
1

Sample 1 Output

2
There are two direct flights: 1→2 and 2→3. There is one flight route from 1 to 2 and 2 to 3, each consisting of a single direct flight. There is one flight route from 1 to 3 (1→2→3).

Sample 2 Input

5
1111
101
01
1

Sample 2 Output

6
There are six direct flights 1→2,1→4,1→5,2→3,3→5,4→5. These result in the following numbers of flight routes:
Flight Route Counts:

            dest
          1 2 3 4 5

       1  0 1 1 1 3 
       2  0 0 1 0 1 
source 3  0 0 0 0 1 
       4  0 0 0 0 1 
       5  0 0 0 0 0
which is equivalent to the sample input after taking all the numbers (mod 2).

HINT

USACO.

Source/Category