#P1726. Absolute Connectivity in Villages

    ID: 15011 Type: Default 1000ms 256MiB

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 NN villages (numbered from 11 to NN) and MM roads. Each road is of one of two types: type 11 indicates a one‐way road, and type 22 indicates a two‐way (bidirectional) road. If there exists a route from village AA to village BB, we denote it as (A,B)(A,B). If both (A,B)(A,B) and (B,A)(B,A) hold, then villages AA and BB are said to be absolutely connected, denoted as A,B\langle A,B\rangle. An absolute connectivity region is defined as a set of villages in which every pair of villages XX and YY satisfies X,Y\langle X,Y\rangle.

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 NN and MM, where NN is the number of villages and MM is the number of roads. Each of the following MM lines contains three integers UU, VV, and TT, where UU and VV denote village numbers and TT denotes the road type (11 for one‐way and 22 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