PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000039. Từ điển con tối thiểu (Sub-dictionary)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Từ điển con tối thiểu (Sub-dictionary)

Sub-dictionary

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Every dictionary word is defined using other dictionary words. Find a smallest independent base set from which every remaining word can be learned once all words in its definition are known.

Input

Integer nn, followed by nn lines, each starting with the defined word and then its definition words.

Output

First line: number of foundation words. Second line: selected words in alphabetical order separated by spaces, or an empty line if none.

Subtasks

  • Subtask 1 (40 points): n≤10n\le10; 2≤n≤1002\le n\le100; lowercase words of length at most 24; each definition contains 1…301\ldots30 words different from the defined word; every referenced word has its own definition line.
  • Subtask 2 (60 points): 2≤n≤1002\le n\le100; lowercase words of length at most 24; each definition contains 1…301\ldots30 words different from the defined word; every referenced word has its own definition line.

Examples

Input

5
aue oizer piqoi oizer
doy oizer hweqlo hweqlo
hweqlo piqoi aue
oizer piqoi
piqoi aue aue

Output

3
aue oizer piqoi

Explanation

The words aue, oizer, and piqoi form a dependency cycle and must be part of the initial base dictionary. The remaining definitions can then be learned in succession.