using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 2");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("2 4");
WillReturn.Add("2 3 10");
WillReturn.Add("1 2 5");
//5
//10
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("10 10");
WillReturn.Add("7 2");
WillReturn.Add("5 8");
WillReturn.Add("8 6");
WillReturn.Add("8 3");
WillReturn.Add("8 9");
WillReturn.Add("9 1");
WillReturn.Add("4 8");
WillReturn.Add("4 10");
WillReturn.Add("8 7");
WillReturn.Add("7 5 12773");
WillReturn.Add("2 6 74733");
WillReturn.Add("1 6 64470");
WillReturn.Add("7 2 41311");
WillReturn.Add("1 9 39776");
WillReturn.Add("4 8 71709");
WillReturn.Add("9 1 23551");
WillReturn.Add("4 6 29181");
WillReturn.Add("3 7 23742");
WillReturn.Add("8 4 54686");
//41311
//12773
//29181
//23742
//64470
//23551
//54686
//0
//23742
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
// 隣接リスト
static Dictionary<long, List<long>> mToNodeListDict = new Dictionary<long, List<long>>();
struct QueryInfoDef
{
internal long U;
internal long V;
internal long C;
}
static List<QueryInfoDef> mQueryInfoList = new List<QueryInfoDef>();
// 最初に登場するInd[ノード]なDict
static Dictionary<long, long> mFirstApperIndDict = new Dictionary<long, long>();
// Level[ノード]なDict
static Dictionary<long, long> mLevelDict = new Dictionary<long, long>();
// 親ノード[ノード]なDict
static Dictionary<long, long> mParentNodeDict = new Dictionary<long, long>();
// スパーステーブルのコンストラクタに使うLevelのList
static List<long> mLevelList = new List<long>();
// スパーステーブル
static SparseTable<long> mInsSparseTable;
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
long NodeCnt = wkArr[0];
foreach (string EachStr in InputList.Skip(1).Take((int)NodeCnt - 1)) {
SplitAct(EachStr);
long FromNode = wkArr[0];
long ToNode = wkArr[1];
if (mToNodeListDict.ContainsKey(FromNode) == false) {
mToNodeListDict[FromNode] = new List<long>();
}
if (mToNodeListDict.ContainsKey(ToNode) == false) {
mToNodeListDict[ToNode] = new List<long>();
}
mToNodeListDict[FromNode].Add(ToNode);
mToNodeListDict[ToNode].Add(FromNode);
}
// 第1引数は、根ノード
// 第2引数は、親ノード (根ノードの場合は -1
// 第3引数は、レベル (根ノードの場合は 1)
ExecEulerTour(1, -1, 1);
for (int I = 0; I <= mEulerTourJyoutaiList.Count - 1; I++) {
long CurrNode = mEulerTourJyoutaiList[I].CurrNode;
mParentNodeDict[CurrNode] = mEulerTourJyoutaiList[I].ParentNode;
if (mFirstApperIndDict.ContainsKey(CurrNode) == false) {
mFirstApperIndDict[CurrNode] = I;
}
mLevelDict[CurrNode] = mEulerTourJyoutaiList[I].Level;
mLevelList.Add(mEulerTourJyoutaiList[I].Level);
}
mInsSparseTable = new SparseTable<long>(mLevelList, true);
foreach (string EachStr in InputList.Skip((int)NodeCnt)) {
SplitAct(EachStr);
QueryInfoDef WillAdd;
WillAdd.U = wkArr[0];
WillAdd.V = wkArr[1];
WillAdd.C = wkArr[2];
mQueryInfoList.Add(WillAdd);
}
mQueryInfoList.Reverse();
// 彩色した色[枝のハッシュ値]なDict
var ColorDict = new Dictionary<long, long>();
foreach (QueryInfoDef EachQueryInfo in mQueryInfoList) {
long NodeU = EachQueryInfo.U;
long NodeV = EachQueryInfo.V;
long LCANode = GetLCANode(NodeU, NodeV);
List<long> EdgeHashList1 = DeriveEdgeHashList(NodeU, LCANode);
List<long> EdgeHashList2 = DeriveEdgeHashList(NodeV, LCANode);
var AllEdgeHashList = new List<long>();
AllEdgeHashList.AddRange(EdgeHashList1);
AllEdgeHashList.AddRange(EdgeHashList2);
foreach (long EachEdgeHash in AllEdgeHashList) {
if (ColorDict.ContainsKey(EachEdgeHash) == false) {
ColorDict[EachEdgeHash] = EachQueryInfo.C;
}
}
// 経路圧縮
foreach (long EachEdgeHash in AllEdgeHashList) {
long Node1, Node2;
GetNode(EachEdgeHash, out Node1, out Node2);
mWarpNodeDict[Node1] = LCANode;
mWarpNodeDict[Node2] = LCANode;
}
}
var sb = new System.Text.StringBuilder();
foreach (string EachStr in InputList.Skip(1).Take((int)NodeCnt - 1)) {
SplitAct(EachStr);
long U = wkArr[0];
long V = wkArr[1];
long EdgeHash = GetEdgeHash(U, V);
if (ColorDict.ContainsKey(EdgeHash)) {
sb.Append(ColorDict[EdgeHash]);
}
else {
sb.Append(0);
}
sb.AppendLine();
}
Console.Write(sb.ToString());
}
// 経路圧縮で、ワープノード[ノード]なDict
static Dictionary<long, long> mWarpNodeDict = new Dictionary<long, long>();
// 開始ノードとLCAノードを引数として、彩色対象の辺のListを返す
static List<long> DeriveEdgeHashList(long pStaNode, long pLCANode)
{
long LCALevel = mLevelDict[pLCANode];
var EdgeList = new List<long>();
if (pStaNode == pLCANode) {
return EdgeList;
}
long CurrNode = pStaNode;
while (mWarpNodeDict.ContainsKey(CurrNode)) {
if (CurrNode == mWarpNodeDict[CurrNode]) break;
CurrNode = mWarpNodeDict[CurrNode];
if (LCALevel >= mLevelDict[CurrNode]) {
return EdgeList;
}
}
while (CurrNode != pLCANode) {
long ParentNode = mParentNodeDict[CurrNode];
if (ParentNode == -1) break;
long EdgeHash = GetEdgeHash(CurrNode, ParentNode);
EdgeList.Add(EdgeHash);
CurrNode = ParentNode;
while (mWarpNodeDict.ContainsKey(CurrNode)) {
if (CurrNode == mWarpNodeDict[CurrNode]) break;
CurrNode = mWarpNodeDict[CurrNode];
if (LCALevel >= mLevelDict[CurrNode]) {
return EdgeList;
}
}
}
return EdgeList;
}
// 辺のハッシュ値を返す
static long GetEdgeHash(long pNode1, long pNode2)
{
long MinNode = Math.Min(pNode1, pNode2);
long MaxNode = Math.Max(pNode1, pNode2);
return MinNode * 1000000 + MaxNode;
}
// 枝のハッシュ値から両端ノードを求める
static void GetNode(long pHash, out long pNode1, out long pNode2)
{
pNode1 = pHash / 1000000;
pNode2 = pHash % 1000000;
}
struct JyoutaiDef_EulerTour
{
internal long ParentNode;
internal long CurrNode;
internal long Level;
}
// DFSを行い、下記の2通りのタイミングでノードをAddする
// 1 最初の訪問時
// 2 子の部分木を訪問時
static List<JyoutaiDef_EulerTour> mEulerTourJyoutaiList = new List<JyoutaiDef_EulerTour>();
static void ExecEulerTour(long pCurrNode, long pParentNode, long pLevel)
{
JyoutaiDef_EulerTour WillAdd;
WillAdd.ParentNode = pParentNode;
WillAdd.CurrNode = pCurrNode;
WillAdd.Level = pLevel;
mEulerTourJyoutaiList.Add(WillAdd);
if (mToNodeListDict.ContainsKey(pCurrNode)) {
foreach (long EachToNode in mToNodeListDict[pCurrNode]) {
if (EachToNode == pParentNode) continue;
ExecEulerTour(EachToNode, pCurrNode, pLevel + 1);
mEulerTourJyoutaiList.Add(WillAdd);
}
}
}
// ノード2つを引数とし、LCAのノードを返す
static long GetLCANode(long pNode1, long pNode2)
{
long Ind1 = mFirstApperIndDict[pNode1];
long Ind2 = mFirstApperIndDict[pNode2];
long LeftInd = Math.Min(Ind1, Ind2);
long RightInd = Math.Max(Ind1, Ind2);
long LCALevel = mInsSparseTable.Query(LeftInd, RightInd);
while (LeftInd + 1 < RightInd) {
long Mid = (LeftInd + RightInd) / 2;
long MinLeft = mInsSparseTable.Query(LeftInd, Mid);
if (MinLeft == LCALevel) {
RightInd = Mid;
}
else {
LeftInd = Mid;
}
}
if (mInsSparseTable.Query(LeftInd, LeftInd) == LCALevel) {
return mEulerTourJyoutaiList[(int)LeftInd].CurrNode;
}
else {
return mEulerTourJyoutaiList[(int)RightInd].CurrNode;
}
}
}
#region SparseTable
// スパーステーブル
internal class SparseTable<Type>
{
private Type[] mInitArr;
private long UB_0;
private long UB_1;
// 最小値か最大値[開始Ind,2の指数]なArr
private Type[,] mMinOrMaxArr;
// Log2の値[2べきな値] なDict
private Dictionary<long, long> mLogDict = new Dictionary<long, long>();
// 最小値を取得するか?
private bool mIsMin = false;
// コンストラクタ
internal SparseTable(IEnumerable<Type> pInitEnum, bool pIsMin)
{
mIsMin = pIsMin;
mInitArr = pInitEnum.ToArray();
UB_0 = mInitArr.GetUpperBound(0);
long Sisuu = 0;
long Beki2 = 1;
while (true) {
mLogDict[Beki2] = Sisuu;
if (Beki2 > mInitArr.Length) {
break;
}
Beki2 *= 2;
Sisuu++;
}
UB_1 = Sisuu;
mMinOrMaxArr = new Type[UB_0 + 1, UB_1 + 1];
// 長さ1の分を初期化
for (long I = 0; I <= UB_0; I++) {
mMinOrMaxArr[I, 0] = mInitArr[I];
}
for (long LoopLength = 2; LoopLength < long.MaxValue; LoopLength *= 2) {
bool WillBreak = true;
long HalfRange = LoopLength / 2;
for (long I = 0; I <= UB_0; I++) {
long StaInd1 = I;
long EndInd1 = I + HalfRange - 1;
long StaInd2 = EndInd1 + 1;
long EndInd2 = StaInd2 + HalfRange - 1;
if (EndInd2 > UB_0) break;
var KouhoList = new List<Type>();
KouhoList.Add(mMinOrMaxArr[I, mLogDict[HalfRange]]);
KouhoList.Add(mMinOrMaxArr[StaInd2, mLogDict[HalfRange]]);
if (mIsMin) {
mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Min();
}
else {
mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Max();
}
WillBreak = false;
}
if (WillBreak) break;
}
}
// 閉区間 [L,R]の最小値または最大値を求める
internal Type Query(long pL, long pR)
{
// 区間を内包する2べきを返す
long Beki2 = 1;
long Sisuu = 0;
while (true) {
long LeftRangeMax = pL + Beki2 - 1;
long RightRangeMin = pR - Beki2 + 1;
if (LeftRangeMax + 1 >= RightRangeMin) {
break;
}
Beki2 *= 2;
Sisuu++;
}
var KouhoList = new List<Type>();
KouhoList.Add(mMinOrMaxArr[pL, Sisuu]);
KouhoList.Add(mMinOrMaxArr[pR - Beki2 + 1, Sisuu]);
if (mIsMin) {
return KouhoList.Min();
}
return KouhoList.Max();
}
}
#endregion