Under construction

Still being built. Things may change

Comparing Answers

Time limit10sMemory limit128 MB

Summary
Given an adjacency matrix with edge counts, verify whether a provided matrix correctly gives the number of length-2 paths between every ordered pair (essentially checking a matrix multiplication), for large n up to 1000 requiring fast I/O.
Level

Medium4 of 10

Topics
Matrix, Brute force, Implementation
Solved
No attempts yet

Problem

In a place in Southwestern Europe, the name of which I do not wish to recall, not long ago there were nn cities connected by one-way roads, possibly with more than one road connecting a city to another, or even a road from a city to itself. As a homework assignment for your geography class, you need to compute the number of paths of length exactly two between each ordered pair of cities. However, you have been too busy celebrating the Spanish victory in the World Cup, so now you are copying the answers from your friend. Before handing in your homework, you would like to make sure his answers are correct.

Input

The input consists of several test cases, separated by single blank lines. Each test case begins with a line containing the integer nn (1≤n≤10001 \le n \le 1000). The following nn lines contain nn elements each; element jj of line ii is the number of roads from city ii to city jj (an integer between 00 and 1010, inclusive). After that come nn more lines, again with nn elements each; element jj of line ii is your friend's answer for the number of length-2 paths from city ii to city jj, an integer between 00 and 100000100000, inclusive.

The test cases finish with a line containing only the number zero (also preceded by a blank line).

Note: the input file is large; use fast I/O routines.

Output

For each test case, output a single line. The line should contain YES if your friend's answers for that assignment are all correct, and NO otherwise.

Examples1

  1. Example 1

    Input
    3
    2 0 1
    1 0 3
    1 1 0
    5 1 2
    5 3 1
    3 0 4
    
    3
    2 0 1
    1 0 3
    1 1 0
    5 1 2
    5 3 2
    3 0 4
    
    0
    
    Expected output
    YES
    NO