Checking if the graph provided by a distance matrix is a tree
We can assume all weights are positive. Otherwise we can merge all pairs of nodes with distance $0$ to get an equivalent new distance matrix (a rigorous proof would be a little verbose here so I omit it).



We can process all distances from small to large. For a distance $d$ between node $u$ and node $v$, check if $u$ and $v$ are connected (initially there is no edge thus every pair of nodes is not connected). If they are not connected, add an edge with weight $d$ between them. Finally we will get a tree and we can check whether this tree satisfies the distance matrix. 

To efficiently determine whether two nodes are connected, we can maintain a disjoint-set data structure where each disjoint set represents a connected component. With this data structure, we can build the tree in $O(N^2)$. In addition, computing all-pair distances in a tree takes $O(N^2)$ time, so the total running time is $O(N^2)$, which is sufficient for the constraints. 



Correctness of the Algorithm

If the algorithm reports "yes", then we indeed build a tree satisfying the distance matrix. 

On the other hand, if there is a tree $T$ satisfying the distance matrix, we claim that our algorithm will build $T$ finally. We prove this claim by mathematical induction. Suppose our algorithm processes the distances of pairs $(u_1,v_1),(u_2,v_2),\ldots$ in order. Denote by $D(u,v)$ the distance between $u$ and $v$ in the distance matrix.


Our algorithm always adds the edge $(u_1,v_1)$ with weight $D(u_1,v_1)$, while there is always an edge $(u_1,v_1)$ with weight $D(u_1,v_1)$ in $T$.
Assume our algorithm agrees with $T$ for pairs $(u_1,v_1),\ldots,(u_{n-1},v_{n-1})$. 

If there is an edge $(u_n,v_n)$ in $T$ (it must have weight $D(u_n,v_n)$), then $u_n$ and $v_n$ are not connected through edges $(u_1,v_1),\ldots,(u_{n-1},v_{n-1})$, which means our algorithm will add this edge by inductive assumption. 

Otherwise, there is a path connecting $u_n$ and $v_n$ with length $D(u_n,v_n)$ in $T$. All of the edges in this path must have weights less than $D(u_n,v_n)$, thus are already added by our algorithm (again, by inductive assumption) when processing $(u_n,v_n)$. This means when our algorithm processes $(u_n,v_n)$, $u_n$ and $v_n$ are already connected, thus our algorithm will not add the edge $(u_n,v_n)$.


Now we have proved our algorithm indeed builds $T$, thus will report "yes".