1 #include<iostream>
2 #include<stack>
3 #include<vector>
4 #include<algorithm>
5 using namespace std;
6
7 int n, m;
8 int dfn[5010], low[5010];
9 bool vis[5010];
10 stack<int>s;
11 int cnt;
12 vector<int>to[5010];
13 int ans;
14 int tot;
15 int res[100010];
16 int num[100010];
17
18 void tarjan(int x)
19 {
20 dfn[x] = low[x] = ++cnt;
21 s.push(x);
22 vis[x] = true;
23 for (int i = 0; i < to[x].size(); i++)
24 {
25 if (!dfn[to[x][i]])
26 {
27 tarjan(to[x][i]);
28 low[x] = min(low[x], low[to[x][i]]);
29 }
30 else if(vis[to[x][i]])
31 {
32 low[x] = min(low[x], dfn[to[x][i]]);
33 }
34 }
35 if (low[x] == dfn[x])
36 {
37 tot++;
38 while (!s.empty() && s.top() != x)
39 {
40 int t = s.top();
41 res[t] = tot;
42 num[tot]++;
43 vis[t] = false;
44 s.pop();
45 };
46 s.pop();
47 res[x] = tot;
48 num[tot]++;
49 vis[x] = false;
50 }
51 }
52
53 int main()
54 {
55 cin >> n >> m;
56 for (int i = 1; i <= m; i++)
57 {
58 int f, a, b;
59 cin >> a >> b >> f;
60 to[a].push_back(b);
61 if (f == 2) to[b].push_back(a);
62 }
63 for (int i = 1; i <= n; i++)
64 {
65 if (!dfn[i]) tarjan(i);
66 }
67 for (int i = 1; i <= tot; i++)
68 ans = max(ans, num[i]);
69 cout << ans << endl;
70 int mark = 0;
71 for (int i = 1; i <= n; i++)
72 {
73 if (!mark && num[res[i]] == ans) mark = res[i];
74 if (num[res[i]] == ans && mark == res[i])
75 {
76 cout << i << " ";
77 }
78 }
79 return 0;
80 }