問題一覧 > 通常問題

No.1605 Matrix Shape

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 99
作問者 : SPD_9X2 / テスター : auaua penguinman
お気に入りにしたユーザー ProblemId : 6367 / 自分の提出
問題文最終更新日: 2021-07-17 00:46:18
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 304 (順位表) の他の問題:

Problem Statement

SPD-kun received a multiset $S$ consisting of $N$ matrices as a reward for running an event.

The shape of the $i$-th ($1 \le i \le N$) matrix is $H_i \times W_i$.

Since many matrices take up a lot of space, SPD-kun intends to multiply all the matrices together into $1$ matrix.

Specifically, he repeats the operation below until $S$ has $1$ element, or there are no longer $2$ elements that can be multiplied.

  • Choose $2$ elements $X$ , $Y$ from $S$ such that $X \cdot Y$ can be computed. Remove $X$ , $Y$ from $S$ and add $X \cdot Y$ to $S$.

Find the number of different possible shapes of the matrix when $S$ finally has $1$ element. However, if $S$ cannot be reduced to $1$ element no matter how the operations are performed, output $0$.


Note that for $2$ matrices $X$ (of shape $H_X \times W_X$) and $Y$ (of shape $H_Y \times W_Y$), $ X \cdot Y $ can be computed if and only if $ W_X = H_Y $, and its shape is then $H_X \times W_Y$.

Also, the shapes of $2$ matrices are said to be different if at least one of the number of rows and the number of columns differs.

Input

$N$
$H_1\ W_1$
$H_2\ W_2$
$\vdots$
$H_N\ W_N$

  • All inputs are integers.
  • $2 \le N \le 2 \times 10^5$
  • $1 \le H_i \le 2 \times 10^5$
  • $1 \le W_i \le 2 \times 10^5$

Output

Print a newline at the end.

Sample

Sample 1
Input
3
1 2
2 2
2 3
Output
1

Sample 2
Input
2
7 8
8 7
Output
2

The shape of the resulting matrix changes depending on the order of multiplication.

Sample 3
Input
5
4 2
6 3
5 9
7 1
8 10
Output
0

No operation can be performed even once.

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