#include using namespace std; typedef long long ll; typedef long double ld; typedef pair ii; typedef vector vi; typedef vector vvi; typedef vector vvvi; typedef vector vvvvi; typedef vector vii; #define x first #define y second #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() class street { public: ll id, s, t, h; ll back; }; ll n, m; vector> adj; vector E; vi res; vector> A; bool pos = true; void dfs(street s) { if (res[s.id] >= 0 && res[s.id] != s.t) { pos = false; return; } else if (res[s.id] >= 0) return; res[s.id] = s.t; if (A[s.s][s.h] >= 2) { for (street t : adj[s.s]) { if (t.h == s.h && t.id != s.id) { dfs(E[t.back]); } } } for (street t : adj[s.t]) { if (s.id == t.id) continue; dfs(t); } } void find_cycle(street s) { if (res[s.id] >= 0) return; static vector visited(n, false); if (visited[s.t]) { dfs(s); return; } visited[s.t] = true; for (street t : adj[s.t]) { if (s.id == t.id) continue; find_cycle(t); } } void run() { cin >> n; m = n; if (m > n) { cout << "impossible" << endl; return; } adj = vector>(n); res = vi(n, -1); A = vector>(n); vii B; for (ll i = 0; i < m; i++) { ll u, v, h; cin >> u >> v >> h; u--; v--; street s1, s2; s1.id = i; s2.id = i; s1.s = u; s2.s = v; s1.t = v; s2.t = u; s1.h = h; s2.h = h; s1.back = E.size()+1; s2.back = E.size(); E.pb(s1); E.pb(s2); adj[u].pb(s1); adj[v].pb(s2); A[u][h]++; A[v][h]++; if (A[u][h] >= 3 || A[v][h] >= 3) { cout << "impossible" << endl; return; } if (A[u][h] >= 2) { B.pb({u, h}); } if (A[v][h] >= 2) { B.pb({v, h}); } } for (ii b : B) { ll u = b.x, h = b.y; for (street s : adj[u]) { if (s.h != h) { dfs(s); if (!pos) { cout << "impossible" << endl; return; } } } } if (!pos) { cout << "impossible" << endl; return; } for (ll u = 0; u < n; u++) { for (street s : adj[u]) { if (res[s.id] < 0) { find_cycle(s); dfs(s); } } } for (ll i = 0; i < m; i++) { cout << res[i]+1 << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); run(); return 0; }