CF928C.Dependency management
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version — positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
- Polycarp's project is chosen;
- Polycarp's project depends (directly or indirectly) on all other projects in the set;
- no two projects share the name;
- for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Polycarp 正在使用 Vaja 语言开发一个项目,并采用一种流行的依赖管理系统——Vamen。在 Vamen 看来,为简化起见,Vaja 项目和库均被视为“项目”。
一个 Vaja 项目具有唯一的、非空的名称,该名称仅由小写拉丁字母组成,长度不超过 10;同时还具有一个版本号,为 1 到 106 之间的正整数。每个项目(注意:项目由其名称与版本号共同唯一确定)可能依赖于其他项目。显然,依赖关系中不存在环。
现给出一组项目描述。其中第一个项目即 Polycarp 当前正在开发的项目。请帮助 Polycarp 确定其项目所依赖的所有项目(包括直接依赖与通过某条依赖链间接依赖的项目)。
Polycarp 的项目可能依赖于某个项目(同名)的多个不同版本。此时将应用冲突解决机制:对每个此类项目,系统选择距 Polycarp 的项目距离最短的版本;若存在多个距离相同的版本,则优先选择**版本号最大(即最新)**的那个版本。该被选中的版本称为“实际版本”(actual version),其余版本及其依赖均被忽略。
更形式化地,需选取满足以下条件的、规模最小的项目集合:
- Polycarp 的项目必须被选中;
- Polycarp 的项目(直接或间接)依赖于该集合中所有其他项目;
- 集合中任意两个项目不能同名;
- 对于集合中任一项目 x 所依赖的任意项目 z,集合中必须包含 z,或包含另一个与 z 同名但到 Polycarp 项目距离更短的项目 y;若存在多个距离相等的选项,则选择版本号最大的那个。
请按字典序输出 Polycarp 项目的所有依赖项目(不包含 Polycarp 自己的项目)。
输入格式
The first line contains an only integer n (1 ≤ n ≤ 1 000) — the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
第一行包含一个整数 n(1≤n≤1000)—— 表示瓦贾(Vaja)中项目的数量。
接下来的若干行描述各个项目。每个项目由一行描述,该行包含其名称与版本号,中间以空格分隔。随后的一行给出该项目的直接依赖项数量(范围为 0 至 n−1),紧接着是各直接依赖项(每行一个),顺序任意。每个依赖项通过其名称与版本号指定。所有项目本身也以任意顺序给出,但其中第一个项目始终是波利卡普(Polycarp)的项目。项目描述之间以一个空行分隔。请参考样例以更好地理解输入格式。
保证不存在循环依赖。
输出格式
Output all Polycarp's project's dependencies in lexicographical order.
按字典序输出 Polycarp 所有项目的依赖项。
输入输出样例
输入#1
4 a 3 2 b 1 c 1 b 2 0 b 1 1 b 2 c 1 1 b 2
输出#1
2 b 1 c 1
输入#2
9 codehorses 5 3 webfrmk 6 mashadb 1 mashadb 2 commons 2 0 mashadb 3 0 webfrmk 6 2 mashadb 3 commons 2 extra 4 1 extra 3 extra 3 0 extra 1 0 mashadb 1 1 extra 3 mashadb 2 1 extra 1
输出#2
4 commons 2 extra 1 mashadb 2 webfrmk 6
输入#3
3 abc 1 2 abc 3 cba 2 abc 3 0 cba 2 0
输出#3
1 cba 2
说明/提示
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «a» (version 3) depends on are painted black.

The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «codehorses» (version 5) depends on are paint it black. Note that «extra 1» is chosen instead of «extra 3» since «mashadb 1» and all of its dependencies are ignored due to «mashadb 2».

第一个样例如下图所示。从 A 指向 B 的箭头表示 B 直接依赖于 A。Polycarp 的项目 «a»(版本 3)所依赖的项目用黑色标出。

第二个样例如下图所示。从 A 指向 B 的箭头表示 B 直接依赖于 A。Polycarp 的项目 «codehorses»(版本 5)所依赖的项目用黑色标出。注意,此处选择 «extra 1» 而非 «extra 3»,原因是 «mashadb 2» 的存在导致 «mashadb 1» 及其所有依赖项均被忽略。
输入解题思路,AI测评打分。不知道怎么写?