#P1726. 上白泽慧音
上白泽慧音
Description
In Gensokyo, Kamishirasawa Keine is a teacher known for her erudition. Due to the Spring Snow incident, many roads in the Human Village area have been blocked by heavy snow, preventing some students from reaching the village where Keine resides. Therefore, Keine decides to choose another village that can gather the largest number of people as the new teaching location.
The Human Village area consists of villages (numbered ) and roads. Roads are of two types: one-way and two-way, denoted by and , respectively. If there exists a path from village to village , then we say can reach , denoted as . When both and hold, we say and are absolutely connected, denoted as . An absolutely connected region is a set of villages such that any two villages in the set satisfy . Your task is to find the largest absolutely connected region and output the villages in this region in increasing order of their indices. If there are multiple largest regions, output the lexicographically smallest one; for example, if and are two largest regions, output .
Input Format
The first line contains two positive integers .
From line to line , each line contains three positive integers . If , there is a one-way road from village to . If , there is a two-way road between villages and . It is guaranteed that each road appears only once.
Output Format
On the first line, output integer, the number of villages in the largest absolutely connected region.
On the second line, output several integers, the indices of the villages in the largest absolutely connected region in increasing order, separated by spaces.
5 5
1 2 1
1 3 2
2 4 2
5 1 2
3 5 1
3
1 3 5
Hint
- For of the testdata, , and .
- For of the testdata, , and .
Translated by ChatGPT 5
京公网安备 11011102002149号