1 #include <algorithm>
2 #include <cstdio>
3
4 using namespace std;
5
6 const int N(5000+15);
7 const int M(50000+5);
8 int n,m,u,v,op,ans_num,max_col;
9
10 int head[N],sumedge;
11 struct Edge
12 {
13 int to,next;
14 Edge(int to=0,int next=0):
15 to(to),next(next){}
16 }edge[M<<1];
17 void ins(int u,int v)
18 {
19 edge[++sumedge]=Edge(v,head[u]);
20 head[u]=sumedge;
21 }
22
23 int tim,dfn[N],low[N];
24 int top,Stack[N],instack[N];
25 int sumcol,col[N],point[N];
26 void DFS(int now)
27 {
28 dfn[now]=low[now]=++tim;
29 Stack[++top]=now;instack[now]=1;
30 for(int i=head[now];i;i=edge[i].next)
31 {
32 int v=edge[i].to;
33 if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
34 else if(instack[v]) low[now]=min(low[now],dfn[v]);
35 }
36 if(low[now]==dfn[now])
37 {
38 col[now]=++sumcol;
39 point[sumcol]++;
40 for(;Stack[top]!=now;top--)
41 {
42 int fro=Stack[top];
43 col[fro]=sumcol;
44 point[sumcol]++;
45 instack[fro]=0;
46 }
47 top--;instack[now]=0;
48 }
49 }
50
51 int main()
52 {
53 scanf("%d%d",&n,&m);
54 for(int i=1;i<=m;i++)
55 {
56 scanf("%d%d%d",&u,&v,&op);
57 if(op==1) ins(u,v);
58 else ins(u,v),ins(v,u);
59 }
60 for(int i=1;i<=n;i++)
61 if(!dfn[i]) DFS(i);
62 for(int i=1;i<=sumcol;i++)
63 if(ans_num<point[i]) max_col=i,ans_num=point[i];
64 printf("%d\n",ans_num);
65 for(int i=1;i<=n;i++)
66 if(col[i]==max_col) printf("%d ",i);
67 return 0;
68 }