問題一覧 > 教育的問題

No.3488 Axioms of Distance

レベル : / AI推定難易度 : 312 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 39
作問者 : 👑 p-adic
お気に入りにしたユーザー ProblemId : 9232 / 自分の提出
問題文最終更新日: 2026-04-03 07:02:23
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 496 (順位表) の他の問題:

Problem Statement

You are given as input a non-negative integer $N$ and a square matrix $D$ of order $N$ with non-negative integer coefficients.

What is a square matrix of order $N$ with non-negative integer coefficients? (click to open)

 

Roughly speaking, it is an arrangement of non-negative integers in $N$ rows and $N$ columns. As with vectors, the whole is enclosed in parentheses to make it easier to read.

For example, when $N = 3$,

$\displaystyle A = \left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array} \right) $

is a concrete example of a square matrix of order $N$ with non-negative integer coefficients.

For positive integers $i$ and $j$ not greater than $N$, the non-negative integer located in the $i$-th row from the top and the $i$-th column from the left of a square matrix $A$ of order $N$ with non-negative integer coefficients is called the $(i,j)$ entry of $A$.

For example, in the matrix $A$ above, the $(1,1)$ entry is $1$, the $(1,2)$ entry is $2$, and the $(2,1)$ entry is $4$.

 

We define $1.$ non-degeneracy, $2.$ symmetry, $3.$ the triangle inequality, and $4.$ the strong triangle inequality of $D$ as the following properties, respectively.

  1. Non-degeneracy: for any positive integers $i$ and $j$ not greater than $N$, $D(i,j) = 0$ and $i = j$ are equivalent.
  2. Symmetry: for any positive integers $i$ and $j$ not greater than $N$, $D(i,j) = D(j,i)$.
  3. Triangle inequality: for any positive integers $i$, $j$, and $k$ not greater than $N$, $D(i,j) \leq D(i,k) + D(k,j)$.
  4. Strong triangle inequality: for any positive integers $i$, $j$, and $k$ not greater than $N$, $D(i,j) \leq \max \{D(i,k),D(k,j)\}$.

Here, for each pair of positive integers $i$ and $j$ not greater than $N$, the $(i,j)$ entry of $D$ (in the $i$-th row from the top) is denoted by $D(i,j)$.

 

$D$ is a good matrix if $D$ satisfies all of $1.$ non-degeneracy, $2.$ symmetry, and $3.$ the triangle inequality.

$D$ is a super good matrix if $D$ satisfies all of $1.$ non-degeneracy, $2.$ symmetry, and $4.$ the strong triangle inequality.

Determine whether $D$ is a good matrix and whether it is a super good matrix.

 

Background

In geometry and number theory, the concepts of metric spaces and ultrametric spaces are used extensively. Let $I_N$ be the set of all positive integers not greater than $N$, and regard $D$ as a function of $2$ variables on $I_N$. Then $D$ is a good matrix if and only if $(I_N,D)$ is a metric space, and $D$ is a super good matrix if and only if $(I_N,D)$ is an ultrametric space. Therefore, this problem is equivalent to determining whether $(I_N,D)$ is a metric space and whether it is an ultrametric space.

Input

The input is given from standard input in $1 + N$ lines in the following format:
  • Line $1$ contains $N$.
  • For each positive integer $i$ not greater than $N$, line $1 + i$ contains $D(i,j)$ for each positive integer $j$ not greater than $N$, in ascending order of $j$, separated by single-byte spaces.
$N$
$D(1,1)$ $\cdots$ $D(1,N)$
$\vdots$
$D(N,1)$ $\cdots$ $D(N,N)$

Constraints

The input satisfies the following constraints:

  • $N$ is an integer satisfying $1 \leq N \leq 10$.
  • For any positive integers $i$ and $j$ not greater than $N$, $D(i,j)$ is an integer satisfying $0 \leq D(i,j) \leq 10$.

Output

On line $1$, print Yes if $D$ is a good matrix, and No if it is not a good matrix.

On line $2$, print Yes if $D$ is a super good matrix, and No if it is not a super good matrix.

Print a newline at the end.

Sample

Sample 1
Input
1
0
Output
Yes
Yes

The square matrix $D = (0)$ of order $1$ with non-negative integer coefficients is both a good matrix and a super good matrix.

For those who know metric spaces, this $D$ represents the trivial metric on $I_N = \{1\}$.

Sample 2
Input
1
1
Output
No
No

The square matrix $D = (1)$ of order $1$ with non-negative integer coefficients does not satisfy $1.$ non-degeneracy, so it is neither a good matrix nor a super good matrix.

Sample 3
Input
3
0 1 2
1 0 1
2 1 0
Output
Yes
No

The square matrix of order $3$ with non-negative integer coefficients

$\displaystyle D = \left( \begin{array}{rcl} \displaystyle 0 &\displaystyle 1 &\displaystyle 2 \\ \displaystyle 1 &\displaystyle 0 &\displaystyle 1 \\ \displaystyle 2 &\displaystyle 1 &\displaystyle 0 \end{array} \right) $

is a good matrix, but it does not satisfy $4.$ the ultra triangle inequality, so it is not a super good matrix.

For those who know real numbers, this $D$ represents the Euclidean metric restricted to $I_N = \{1,2,3\}$.

Sample 4
Input
3
0 2 1
2 0 2
1 2 0
Output
Yes
Yes

The square matrix of order $3$ with non-negative integer coefficients

$\displaystyle D = \left( \begin{array}{rcl} \displaystyle 0 &\displaystyle 2 &\displaystyle 1 \\ \displaystyle 2 &\displaystyle 0 &\displaystyle 2 \\ \displaystyle 1 &\displaystyle 2 &\displaystyle 0 \end{array} \right) $

is both a good matrix and a super good matrix.

For those who know $p$-adic numbers, this $D$ represents $2$ times the $2$-adic metric, restricted to $I_N = \{1,2,3\}$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。