QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21234. 学习语言

Statistics

Farmer John 的 $N$ 头奶牛($2 \le N \le 10,000$)编号 $1..N$,精通若干种语言。语言共 $M$ 种($1 \le M \le 30,000$),编号 $1..M$。奶牛 $i$ 会 $K_{i}$ 种($1 \le K_{i} \le M$),即 $L_{i1}$、$L_{i2}$、……、$L_{iK_i}$($1 \le L_{ij} \le M$)。奶牛没那么聪明,所以所有奶牛 $i$ 的 $K_{i}$ 之和至多 $100,000$。

两头只有会共同语言才可直接交谈,但可传递消息,必要时翻译。即 $A$ 与 $B$ 能交谈当且仅当存在序列 $T_{1}$、$T_{2}$、……、$T_{k}$,使 $A$ 与 $T_{1}$ 会共同语言,$T_{1}$ 与 $T_{2}$ 会共同语言,依此类推,$T_{k}$ 与 $B$ 会共同语言。

他希望奶牛更善交际,每头都能与任意其他奶牛交流。可买书教任一奶牛任意语言。作为节俭农夫,他想买最少书让所有奶牛互通。请确定:

  • 需购买的最少书数;
  • 能达成目标的任意一组给奶牛的书,顺序任意;程序将评判输出。

例如 Alberta、Bessie、Contessa 三头与语言 $1$、$2$、$3$。Alberta 会 $2$、$3$,Bessie 会 $2$,Contessa 会 $1$。目前 Alberta 与 Bessie 可交流,而 Contessa 独自一头。

                   #1   #2   #3
       Alberta           x    x
       Bessie            x
       Contessa     x

可给 Contessa 一本书教语言 $2$,使所有奶牛会同一语言,都可互通。

也可教 Contessa 语言 $3$。并非都懂同一语言,但仍合法:Contessa 想与 Bessie 交流时,可通过同样懂 $3$ 的 Alberta。还有其他方案,任意合法替代方案均接受。

输入格式

  • 第 $1$ 行:空格分隔整数 $N$ 和 $M$。

  • 第 $2..N+1$ 行:第 $i+1$ 行用 $(K_i)+1$ 个空格分隔整数 $K_{i}$、$L_{i1}$、$L_{i2}$、……、$L_{iK_i}$ 描述奶牛 $i$ 会的语言。

输出格式

  • 第 $1$ 行:整数,表示最少购买书数。

  • 第 $2..B+1$ 行:第 $i+1$ 行含两个空格分隔整数:语言编号,以及收到书 $i$ 的奶牛编号。多解输出任一个。

样例

输入格式 1

3 3
2 3 2
1 2
1 1

输出格式 1

1
2 3

出题人:Albert Gu,2011

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.