#!/usr/bin/env python3 import sys sys.setrecursionlimit(1000000) n = int(input()) g = [[] for _ in range(n)] for i in range(n): u, v, h = map(int, input().split()) g[u - 1].append((v - 1, h, i)) g[v - 1].append((u - 1, h, i)) ans = [0] * n # Every node is has a 1-end. # There is a single cycle, and a forest connected to it. # All edges in the forest have the 1 away from the cycle. # The cycle can be directed in only 2 directions. # 1. find the cycle. done = [False] * n cycle = [] is_cycle = [False] * n def dfs(u, p): if done[u]: return u done[u] = True for e in g[u]: v = e[0] if v != p: cc = dfs(v, u) if cc is None: continue if cc != -1: is_cycle[u] = True cycle.append((u, v, e[1], e[2])) if cc == u: cc = -1 return cc dfs(0, -1) def impossible(): print("impossible") exit(0) values = [[] for _ in range(n)] cnt = 0 def fill_tree(u, p): global cnt for e in g[u]: v = e[0] if is_cycle[v] or v == p: continue if e[1] in values[u]: impossible() values[u].append(e[1]) assert ans[e[2]] == 0 cnt += 1 ans[e[2]] = v + 1 fill_tree(v, u) # Fill attached trees for i in range(n): if is_cycle[i]: fill_tree(i, -1) # print(cycle) # print(is_cycle) # print(values) # print(ans) assert cnt == n - len(cycle) # Fill cycle, forward def test(): for u, v, h, i in cycle: # print("edge", u, v, h, i) if h in values[u]: return False ans[i] = v + 1 print(*ans) exit(0) test() for j in range(len(cycle)): u, v, h, i = cycle[j] cycle[j] = (v, u, h, i) test() impossible()