Under construction

Still being built. Things may change

Linker

Interview

Time limit1sMemory limit128 MB

Summary
Given modules with export/import tables and an entry symbol, find the reachable non-redundant modules, duplicate exports used by them, and unresolved imports.
Level

Medium5 of 10

Topics
Graph, BFS, Hash map
Solved
No attempts yet

Problem

Computer programs are usually built from several modules, and compiling such a program is normally done in two stages.

First, each source module is compiled independently into an object module. An object module has three parts:

  • the compiled program code,
  • an export table,
  • an import table.

The export table lists the symbols (for example, variable or function names) that this module defines and that other modules are allowed to use. The import table lists the external symbols that this module depends on.

After every module has been compiled, a program called the linker combines them into a single executable.

Every program has an entry point: the place where execution begins. The entry point is a symbol, and the module that "contains" it is the module that exports that symbol. To make the executable smaller, the linker keeps only the module that contains the entry point together with every module it depends on, directly or transitively; all other modules are called redundant.

Dependencies are expressed through symbols: a module depends on another module when it imports a symbol that the other module exports. If a symbol is exported by several modules, then a module importing that symbol depends on all of them.

Given the description of every module, report:

  • all non-redundant modules,
  • all duplicate exports: symbols that are exported by two or more modules and are imported by at least one non-redundant module,
  • all unresolved imports: symbols that are imported by at least one non-redundant module but are not exported by any module.

If the entry-point symbol is not exported by any module, it is treated as an unresolved import as well; in that case no module contains the entry point, so there are no non-redundant modules.

Input

The first line contains the name of the entry point.

The second line contains the number of modules NN (1≤N≤1001 \le N \le 100).

After that come NN blocks, one per module:

  • the first line of a block is the module's name;
  • the next line contains the number of exports EE (0≤E≤1000 \le E \le 100), followed by EE lines, each with one distinct export symbol;
  • the next line contains the number of imports II (0≤I≤1000 \le I \le 100), followed by II lines, each with one distinct import symbol.

Every name in this problem is a non-empty string of at most 3030 characters made up of latin letters and digits.

Output

Print the non-redundant modules, then the duplicate exports, then the unresolved imports. To make the answer unique, print each group in a fixed order:

  • First print MRM_R, the number of non-redundant modules, followed by MRM_R lines with their names in the order the modules appear in the input.
  • Then print MEM_E, the number of duplicate export symbols, followed by MEM_E lines with those symbols in ascending ASCII order.
  • Then print MIM_I, the number of unresolved import symbols, followed by MIM_I lines with those symbols in ascending ASCII order.

In ASCII order, digits come before uppercase letters, which come before lowercase letters.

Examples3

  1. Example 1

    Input
    entry
    4
    main
    1
    entry
    1
    work
    first
    1
    work
    1
    input
    second
    1
    work
    1
    output
    redundant
    1
    foo
    1
    bar
    
    Expected output
    3
    main
    first
    second
    1
    work
    2
    input
    output
    
  2. Example 2

    Input
    start
    1
    foo
    1
    a
    1
    b
    
    Expected output
    0
    0
    1
    start
    
  3. Example 3

    Input
    main
    3
    A
    1
    main
    1
    x
    B
    1
    x
    0
    C
    1
    y
    0
    
    Expected output
    2
    A
    B
    0
    0