시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
3 초 (추가 시간 없음) | 1024 MB | 21 | 9 | 8 | 40.000% |
Graph problems are popular in competitive programming, and problems related to distanceis and trees appear frequently. Let us start with some definitions.
A set is a collection of distinct elements. An undirected simple graph $G$ is a pair $(V, E)$, where $V$ is a set and $E$ is a set of unordered pairs of $V$’s elements. For a graph $G = (V, E)$, we call $V$ as $G$’s vertex set and $E$ as $G$’s edge set. Elements in $V$ are vertices, and elements in $E$ are edges.
Let $u$ and $v$ be vertices in $V$. A path from $u$ to $v$ of length $k$ is a sequence of edges $e_1, e_2, \dots , e_k ∈ E$ such that there exists a sequence of distinct vertices, $v_1, \dots , v_{k+1}$, satisfying the following conditions.
If $p$ is a path from $u$ to $v$, then $u$ and $v$ are connected by $p$.
We can define distances and trees now. Given two vertices $u, v ∈ V$, the distance $δ(u, v)$ from $u$ to $v$ is $0$ if $u = v$. If there exists a path from $u$ to $v$, then $δ(u, v)$ is the minimum number of edges required to form a path from $u$ to $v$. Otherwise, $δ(u, v) = ∞$. A tree is an undirected graph in which any distinct two vertices $u$ and $v$ are connected by exactly one path.
Danny gives you a sequence of non-negative integers $d_1, d_2, \dots , d_n$ and asks you to construct a tree $G_T = (V_T , E_T )$ satisfying the following conditions.
If there exists such tree graph, please output the edge set $E_T$. Otherwise, output $-1$.
The first line contains a positive integer $n$ indicating the number of vertices of the tree to be constructed. The second line contains $n$ non-negative integers $d_1, \dots , d_n$, the sequence given by Danny.
If there does not exist such a tree $G_T$, output $-1$. Otherwise, output $n - 1$ lines to represent the edge set $E_T$. The $i$-th line should contain two space-separated integers $u_i$ and $v_i$. The $i$-th edge in $E_T$ should be $\{p_{u_i} , p_{v_i}\}$. If there are multiple solutions, you may output any of them.
5 0 1 2 1 3
-1
5 1 1 0 1 1
1 3 3 2 3 4 5 3
ICPC > Regionals > Asia Pacific > Taiwan > Taiwan Online Programming Contest > TOPC 2022 D번