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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 , followed by 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): ; ; lowercase words of length at most 24; each definition contains words different from the defined word; every referenced word has its own definition line.
- Subtask 2 (60 points): ; lowercase words of length at most 24; each definition contains 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.