#include #include #include using namespace std; struct nod { int val, cost; }; vector grf[100001]; queue coada; int d[100001]; bool ok; int ccost[100001]; bool incoada[100001], visited[100001], viz2[100001]; int starters[100001], noduri[100001], nd, nrnd, st, nrfin; bool okglob; set stfin, stf; set::iterator it; void dfs(int k) { int i, j; stfin.insert(k); viz2[k]=1; for(j=1; j<=nrnd; j++) { if(k==noduri[j]) { for(it=stfin.begin(); it!=stfin.end(); it++) { stf.insert((*it)); } stf.insert(k); } } for(i=0; i>n>>m; for(i=1; i<=m; i++) { cin>>a>>b>>c; nod act; act.val=b; act.cost=c; grf[a].push_back(act); } for(i=1; i<=n; i++) { d[i]=10000000; } for(nd=1; nd<=n; nd++) { if(visited[nd]) continue; else { st++; starters[st]=nd; } coada.push(nd); d[nd]=0; incoada[nd]=1; while(!coada.empty() && !ok) { int cx=coada.front(); visited[cx]=1; coada.pop(); incoada[cx]=0; for(i=0; in) { nrnd++; ok=true; noduri[nrnd]=grf[cx][i].val; nrnd++; noduri[nrnd]=cx; } else { coada.push(grf[cx][i].val); ccost[grf[cx][i].val]++; incoada[grf[cx][i].val]=1; } } } } } while(!coada.empty()) { coada.pop(); } ok=false; } for(i=1; i<=st; i++) { dfs(starters[i]); } cout<