#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 NN villages (numbered 1N1 \cdots N) and MM roads. Roads are of two types: one-way and two-way, denoted by 11 and 22, respectively. If there exists a path from village AA to village BB, then we say AA can reach BB, denoted as (A,B)(A,B). When both (A,B)(A,B) and (B,A)(B,A) hold, we say AA and BB are absolutely connected, denoted as A,B\langle A,B\rangle. An absolutely connected region is a set of villages such that any two villages X,YX, Y in the set satisfy X,Y\langle X,Y\rangle. 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 1,3,41,3,4 and 2,5,62,5,6 are two largest regions, output 1,3,41,3,4.

Input Format

The first line contains two positive integers N,MN, M.

From line 22 to line M+1M+1, each line contains three positive integers a,b,ta, b, t. If t=1t = 1, there is a one-way road from village aa to bb. If t=2t = 2, there is a two-way road between villages aa and bb. It is guaranteed that each road appears only once.

Output Format

On the first line, output 11 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 60%60\% of the testdata, 1N2001 \le N \le 200, and 0M1040 \le M \le 10^4.
  • For 100%100\% of the testdata, 1N5×1031 \le N \le 5\times 10^3, and 0M5×1040 \le M \le 5\times 10^4.

Translated by ChatGPT 5