C. Dependency management

Codeforces
IDCF928C
Time4000ms
Memory256MB
Difficulty
graphsimplementation
English · Original
Chinese · Translation
Formal · Original
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. ## Input 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. ## Output Output all Polycarp's project's dependencies in lexicographical order. [samples] ## Note 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. <center>![image](https://espresso.codeforces.com/b126bd144d137361cf0c525ea2ef17e853786a7c.png)</center>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_». <center>![image](https://espresso.codeforces.com/12dfe0292e2e171eca9786b26ec6682a0425b090.png)</center>
Polycarp 正在使用 Vaja 语言开发一个项目,并使用名为 Vamen 的流行依赖管理系统的项目。从 Vamen 的角度来看,Vaja 项目和库都被视为项目以简化处理。 Vaja 中的一个项目具有唯一的非空名称,由不超过 #cf_span[10] 个的小写拉丁字母组成,以及一个版本——一个从 #cf_span[1] 到 #cf_span[106] 的正整数。每个项目(请注意,它由名称和版本共同确定)可能依赖于其他项目。显然,不存在循环依赖。 你将获得一份项目描述列表。*给定的首个*项目是 Polycarp 目前正在开发的项目。请帮助 Polycarp 确定他的项目所依赖的所有项目(直接依赖或通过某种链式依赖)。 Polycarp 的项目可能依赖于某个项目的两个不同版本。在这种情况下,将应用冲突解决机制:对于每个这样的项目,系统会选择距离 Polycarp 项目最近的版本。如果有多个选项,则优先选择版本号更大的(更新的)版本。该版本被视为有效版本;*其他版本及其依赖项将被忽略*。 更正式地,选择一个尽可能小的项目集合,使得满足以下条件: 请按字典序输出 Polycarp 项目的全部依赖项(Polycarp 的项目本身不应被输出)。 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 1 000])——Vaja 中的项目数量。 接下来的行包含项目描述。每个项目由一行描述,包含其名称和版本,以空格分隔。下一行给出直接依赖的数量(从 #cf_span[0] 到 #cf_span[n - 1]),随后是这些依赖项(每行一个),顺序任意。每个依赖项由其名称和版本指定。项目给出的顺序是任意的,但第一个项目始终是 Polycarp 的。项目描述之间用一个空行分隔。请参考示例以获得更好的理解。 保证不存在循环依赖。 请按字典序输出 Polycarp 项目的全部依赖项。 第一个示例见下图。从 #cf_span[A] 到 #cf_span[B] 的箭头表示 #cf_span[B] 直接依赖于 #cf_span[A]。Polycarp 的项目 «_a_»(版本 #cf_span[3])所依赖的项目被涂成黑色。 第二个示例也见下图。从 #cf_span[A] 到 #cf_span[B] 的箭头表示 #cf_span[B] 直接依赖于 #cf_span[A]。Polycarp 的项目 «_codehorses_»(版本 #cf_span[5])所依赖的项目被涂成黑色。注意,选择 «_extra 1_» 而非 «_extra 3_»,因为 «_mashadb 1_» 及其所有依赖项由于 «_mashadb 2_» 而被忽略。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 1 000])——Vaja 中的项目数量。接下来的行包含项目描述。每个项目由一行描述,包含其名称和版本,以空格分隔。下一行给出直接依赖的数量(从 #cf_span[0] 到 #cf_span[n - 1]),随后是这些依赖项(每行一个),顺序任意。每个依赖项由其名称和版本指定。项目给出的顺序是任意的,但第一个项目始终是 Polycarp 的。项目描述之间用一个空行分隔。请参考示例以获得更好的理解。保证不存在循环依赖。 ## Output 请按字典序输出 Polycarp 项目的全部依赖项。 [samples] ## Note 第一个示例见下图。从 #cf_span[A] 到 #cf_span[B] 的箭头表示 #cf_span[B] 直接依赖于 #cf_span[A]。Polycarp 的项目 «_a_»(版本 #cf_span[3])所依赖的项目被涂成黑色。第二个示例也见下图。从 #cf_span[A] 到 #cf_span[B] 的箭头表示 #cf_span[B] 直接依赖于 #cf_span[A]。Polycarp 的项目 «_codehorses_»(版本 #cf_span[5])所依赖的项目被涂成黑色。注意,选择 «_extra 1_» 而非 «_extra 3_»,因为 «_mashadb 1_» 及其所有依赖项由于 «_mashadb 2_» 而被忽略。
**Definitions** Let $ P = \{ (name_i, version_i, deps_i) \mid i \in \{1, \dots, n\} \} $ be the set of projects, where: - $ name_i \in \Sigma^{*} $, $ \Sigma = \{a, b, \dots, z\} $, $ 1 \leq |name_i| \leq 10 $, - $ version_i \in \mathbb{Z} $, $ 1 \leq version_i \leq 10^6 $, - $ deps_i \subseteq P $ is the set of direct dependencies of project $ i $, represented as pairs $ (name, version) $. Let $ P_0 = (name_0, version_0) \in P $ be Polycarp’s project (the first project in input). **Constraints** 1. $ 1 \leq n \leq 1000 $ 2. No cyclic dependencies in the dependency graph. 3. Each dependency is specified by a valid $ (name, version) $ pair from $ P $. **Objective** Find the minimal set $ D \subseteq P \setminus \{P_0\} $ such that: - Every project in $ D $ is reachable from $ P_0 $ via dependency chains. - For each project name $ m $, if multiple versions $ (m, v_1), (m, v_2), \dots $ are reachable, select **only one**: - The version minimizing the **distance** (number of edges) from $ P_0 $, - If tie, select the one with **maximum version**. - All other versions and their dependencies are excluded. Output all projects in $ D $, sorted lexicographically by $ name $, then by $ version $.
Samples
Input #1
4
a 3
2
b 1
c 1
 
b 2
0
 
b 1
1
b 2
 
c 1
1
b 2
Output #1
2
b 1
c 1
Input #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
Output #2
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input #3
3
abc 1
2
abc 3
cba 2

abc 3
0

cba 2
0
Output #3
1
cba 2
API Response (JSON)
{
  "problem": {
    "name": "C. Dependency management",
    "description": {
      "content": "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 projec",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF928C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 projec...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Polycarp 正在使用 Vaja 语言开发一个项目,并使用名为 Vamen 的流行依赖管理系统的项目。从 Vamen 的角度来看,Vaja 项目和库都被视为项目以简化处理。\n\nVaja 中的一个项目具有唯一的非空名称,由不超过 #cf_span[10] 个的小写拉丁字母组成,以及一个版本——一个从 #cf_span[1] 到 #cf_span[106] 的正整数。每个项目(请注意,它由名称和版...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ P = \\{ (name_i, version_i, deps_i) \\mid i \\in \\{1, \\dots, n\\} \\} $ be the set of projects, where:  \n- $ name_i \\in \\Sigma^{*} $, $ \\Sigma = \\{a, b, \\dots, z\\} $, $ 1 \\leq |name...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments