struct N{int i,j,w;}; vectord[1d5]; int e[1d5]; int h[1d5]; int o[1d5]; int z[1d5]; int dir; bool f(int i,int l,int g){ if(e[i]==0){ e[i]=1; h[i]=g; o[i]=l; rep(j,d[i].size()){ z[l]=d[i][j].i; if(f(d[i][j].j,l+1,g+d[i][j].w)){ if(e[i]==3){ if(dir){ rep(k,l,o[i]-1){ wtSp(z[k]+1); } wt(z[o[i]-1]+1); }else{ rrep(k,l+1,o[i]){ wtSp(z[k]+1); } wt(z[l]); } exit(0); } return true; } } e[i]=2; } if(e[i]==1){ if(g!=h[i]){ wt(l-o[i]); wt(i+1); dir=g>h[i]; o[i]=l; e[i]=3; return true; } } return false; } { int@n,@m,@(u,v,w)[m]; rep(i,m){ d[u[i]-1].push_back({i,v[i]-1,w[i]}); d[v[i]-1].push_back({i,u[i]-1,-w[i]}); } rep(i,n){ f(i,0,0); } wt("-1"); }