QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 2048 MB Total points: 100

#2193. 仙人掌复仇

Statistics

NE(E)RC 在往年曾多次出现关于仙人掌图(cactus)的题目——仙人掌图是一种连通的无向图,其中每条边最多属于一个简单环。直观地说,仙人掌图是树的一种推广,允许存在一些环。NEERC 2005 题目中最初使用的传统仙人掌图示例如样例部分的第二张图所示。

给定 $n$ 个整数 $d_1, d_2, \dots, d_n$。请构造一个具有 $n$ 个顶点的仙人掌图,使得顶点 $i$ 的度数为 $d_i$(即恰好有 $d_i$ 条关联边),或者确定不存在这样的仙人掌图。图中不允许有重边和自环。

输入格式

第一行包含一个整数 $n$ ($2 \le n \le 2\,000$),表示仙人掌图的顶点数。 第二行包含 $n$ 个整数 $d_1, d_2, \dots, d_n$ ($1 \le d_i \le n - 1$),表示期望的顶点度数。

输出格式

如果无法构造满足条件的仙人掌图,输出一个整数 $-1$。 否则,按照惯例,将构造出的仙人掌图以一组边不相交的路径形式输出。 第一行输出一个整数 $m$,表示路径的数量。接下来的 $m$ 行,每行包含图中的一条路径。路径应以一个整数 $k_i$ ($k_i \ge 2$) 开头,后跟 $k_i$ 个 $1$ 到 $n$ 之间的整数。这些 $k_i$ 个整数表示该路径上连续的顶点。路径中相邻的顶点必须不同。路径可以多次经过同一个顶点,但仙人掌图中的每一条边在整个输出中必须恰好被遍历一次。

样例

输入格式 1

5
2 2 3 2 1

输出格式 1

4
2 1 2
2 2 3
2 3 1
3 3 4 5

说明

输入格式 2

4
3 3 2 2

输出格式 2

-1

输入格式 3

6
1 2 1 1 2 1

输出格式 3

-1

输入格式 4

15
1 4 3 2 2 2 2 2 4 4 2 2 2 2 2

输出格式 4

3
9 1 2 3 4 5 6 7 8 3
7 2 9 10 11 12 13 10
5 2 14 9 15 10

说明

在第二个和第三个样例中,虽然存在满足给定度数条件的图,但它们都不是仙人掌图。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.