#include #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace atcoder; using namespace std; typedef long long ll; struct UnionFindWeight { vector par; vector rank; vector diff_weight; UnionFindWeight(ll n = 1, ll SUM_UNITY = 0) { reset(n, SUM_UNITY); } void reset(ll n = 1, ll SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (ll i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } ll root(ll x) { if (par[x] == x) { return x; } else { ll r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return diff_weight[x]; } bool same(ll x, ll y) { return root(x) == root(y); } bool unite(ll x, ll y, ll w) { // x ----(距離 w)---- y w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } ll dist(ll x, ll y) { return weight(y) - weight(x); } }; template struct Graph { struct edge { int to; T cost; }; int N; vector> G; vector dist; vector prever; Graph(int n) { init(n); } T inf() { if (is_same_v) return 1e9; else return 1e18; } T zero() { return T(0); } void init(int n) { N = n; G.resize(N); dist.resize(N, inf()); } void add_edge(int s, int t, T cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(int s) { rep(i, 0, N) dist[i] = inf(); prever = vector(N, -1); dist[s] = zero(); priority_queue, vector>, greater>> q; q.push({zero(), s}); while (!q.empty()) { int now; T nowdist; tie(nowdist, now) = q.top(); q.pop(); if (dist[now] < nowdist) continue; for (auto e : G[now]) { T nextdist = nowdist + e.cost; // 次の頂点への距離 if (dist[e.to] > nextdist) { prever[e.to] = now; dist[e.to] = nextdist; q.push({dist[e.to], e.to}); } } } } vector get_path(int t) { // tへの最短路構築 if (dist[t] >= inf()) return {-1}; vector path; for (; t != -1; t = prever[t]) { path.push_back(t); } reverse(path.begin(), path.end()); return path; } }; int main() { int n, m; cin >> n >> m; UnionFindWeight uf(n); Graph gr(n); map, int> ma; rep(i, 0, m) { int l, r, d; cin >> l >> r >> d; l--; r--; if (uf.same(l, r)) { if (uf.dist(l, r) != d) { if (uf.dist(l, r) < d) { swap(l, r); } gr.dijkstra(l); auto path = gr.get_path(r); vector ans; rep(j, 0, path.size() - 1) { ans.push_back(ma[{path[j], path[j + 1]}]); } ans.push_back(i); cout << ans.size() << endl; cout << l + 1 << endl; rep(j, 0, ans.size()) { cout << ans[j] + 1 << ' '; } cout << endl; return 0; } } else { uf.unite(l, r, d); gr.add_edge(l, r, 1); gr.add_edge(r, l, 1); ma[{l, r}] = i; ma[{r, l}] = i; } } cout << "-1" << endl; }