#P1726. Absolute Connectivity in Villages
Absolute Connectivity in Villages
Absolute Connectivity in Villages
In Gensokyo, the teacher Ueshiba Keine is famous for her extensive knowledge. Due to an unusual snowfall anomaly, many roads in the Human Village are blocked by heavy snow, preventing some students from reaching Keine's village. Therefore, she decides to relocate her classes to a village that can gather the maximum number of students.
The Human Village consists of villages (numbered from to ) and roads. Each road is of one of two types: type indicates a one‐way road, and type indicates a two‐way (bidirectional) road. If there exists a route from village to village , we denote it as . If both and hold, then villages and are said to be absolutely connected, denoted as . An absolute connectivity region is defined as a set of villages in which every pair of villages and satisfies .
Your task is to find the largest absolute connectivity region and output its village numbers in increasing order. If there are multiple regions of maximum size, output the lexicographically smallest one (e.g. between regions {1,3,4} and {2,5,6}, output {1,3,4}).
inputFormat
The first line contains two integers and , where is the number of villages and is the number of roads. Each of the following lines contains three integers , , and , where and denote village numbers and denotes the road type ( for one‐way and for two‐way).
outputFormat
Output the village numbers in the largest absolute connectivity region in increasing order, separated by spaces. In case of a tie (multiple regions with maximum size), output the lexicographically smallest sequence.
sample
4 4
1 2 1
2 1 1
3 4 2
4 3 2
1 2