#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; // S : group template struct weighted_union_find { public: weighted_union_find() : _n(0) {} explicit weighted_union_find(int n) : _n(n), parent_or_size(n, -1), weight(n, e()) {} int merge(int a, int b, S w) { // W(a->b) = Wa^-1 Wb = w assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); assert(x != y); if (-parent_or_size[x] < -parent_or_size[y]) { std::swap(x, y); std::swap(a, b); w = inv(w); } // Wa^-1 Wy Wb = w // Wy = Wa w Wb^-1 weight[y] = op(op(weight[a], w), inv(weight[b])); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } S diff(int a, int b) { // W(a->b) = Wa^-1 Wb int x = leader(a), y = leader(b); assert(x == y); return op(inv(weight[a]), weight[b]); } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); int pre = -1; while (parent_or_size[a] >= 0) { int na = parent_or_size[a]; parent_or_size[a] = pre; pre = a; a = na; } S w = e(); while (pre != -1) { w = op(w, weight[pre]); weight[pre] = w; int npre = parent_or_size[pre]; parent_or_size[pre] = a; pre = npre; } return a; } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector> groups() { std::vector leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector parent_or_size; std::vector weight; }; ll op(ll x, ll y) { return x + y; } ll e() { return 0; } ll inv(ll x) { return -x; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; struct E { int u, v, w; }; vector es(m); for (auto& [u, v, w] : es) cin >> u >> v >> w, u--, v--; weighted_union_find uf(n); vector> to(n); for (int i = -1; auto [u, v, w] : es) { i++; if (uf.same(u, v)) { if (uf.diff(u, v) != w) { VI st; auto dfs = [&](auto&& self, int u, int p) -> bool { if (u == v) return true; for (auto [v, eid] : to[u]) if (v != p) { st.emplace_back(eid); if (self(self, v, u)) return true; st.pop_back(); } return false; }; bool res = dfs(dfs, u, -1); assert(res); st.emplace_back(i); ll now = uf.diff(u, v) - w; if (now < 0) reverse(all(st)); cout << ssize(st) << '\n'; cout << u + 1 << '\n'; rep(i, ssize(st)) cout << st[i] + 1 << " \n"[i + 1 == ssize(st)]; return 0; } } else { uf.merge(u, v, w); to[u].emplace_back(v, i); to[v].emplace_back(u, i); } } cout << -1 << '\n'; }