private static void Main() { int[,] edges = new int[9999, 3]; int remainingTestCases = FastIO.ReadNonNegativeInt(); while (remainingTestCases-- > 0) { int vertexCount = FastIO.ReadNonNegativeInt(); for (int i = 0; i < vertexCount - 1; ++i) { edges[i, 0] = FastIO.ReadNonNegativeInt() - 1; // first vertex ID edges[i, 1] = FastIO.ReadNonNegativeInt() - 1; // second vertex ID edges[i, 2] = FastIO.ReadNonNegativeInt(); // weight } var solver = new QTREE(vertexCount, edges); char instruction; while ((instruction = FastIO.ReadInstruction()) != 'D') { if (instruction == 'Q') { FastIO.WriteNonNegativeInt(solver.Query( firstVertexID: FastIO.ReadNonNegativeInt() - 1, secondVertexID: FastIO.ReadNonNegativeInt() - 1)); FastIO.WriteLine(); } else { solver.Change( edgeID: FastIO.ReadNonNegativeInt() - 1, weight: FastIO.ReadNonNegativeInt()); } } } FastIO.Flush(); }
private static void Main() { int remainingTestCases = FastIO.ReadNonNegativeInt(); while (remainingTestCases-- > 0) { _vertexCount = FastIO.ReadNonNegativeInt(); for (int vertexID = 0; vertexID < _vertexCount; ++vertexID) { _verticesNeighbors[vertexID] = new List <int>(); _verticesEdgeWeights[vertexID] = new List <int>(); _verticesEdges[vertexID] = new List <int>(); } for (int edgeID = 0; edgeID < _vertexCount - 1; ++edgeID) { int firstVertexID = FastIO.ReadNonNegativeInt() - 1; int secondVertexID = FastIO.ReadNonNegativeInt() - 1; int edgeWeight = FastIO.ReadNonNegativeInt(); _verticesNeighbors[firstVertexID].Add(secondVertexID); _verticesNeighbors[secondVertexID].Add(firstVertexID); _verticesEdgeWeights[firstVertexID].Add(edgeWeight); _verticesEdgeWeights[secondVertexID].Add(edgeWeight); _verticesEdges[firstVertexID].Add(edgeID); _verticesEdges[secondVertexID].Add(edgeID); } BuildRootedStructure( parentVertexID: -1, vertexID: 0, depth: 0, costToRoot: 0); BuildAncestorTable(); char instruction; while ((instruction = FastIO.ReadInstruction()) != 'S') { if (instruction == 'D') { FastIO.WriteNonNegativeInt(GetDistanceBetween( firstVertexID: FastIO.ReadNonNegativeInt() - 1, secondVertexID: FastIO.ReadNonNegativeInt() - 1)); FastIO.WriteLine(); } else { FastIO.WriteNonNegativeInt(GetKthVertexBetween( firstVertexID: FastIO.ReadNonNegativeInt() - 1, secondVertexID: FastIO.ReadNonNegativeInt() - 1, k: FastIO.ReadNonNegativeInt()) + 1); FastIO.WriteLine(); } } } FastIO.Flush(); }
private static void Main() { int remainingTestCases = FastIO.ReadNonNegativeInt(); while (remainingTestCases-- > 0) { _vertexCount = FastIO.ReadNonNegativeInt(); for (int vertexID = 0; vertexID < _vertexCount; ++vertexID) { _verticesNeighbors[vertexID] = new List <int>(); _verticesEdgeWeights[vertexID] = new List <int>(); _verticesEdges[vertexID] = new List <int>(); // Can't be more chain heads than vertices, so this definitely resets enough. _chainsHeadVertices[vertexID] = -1; } for (int edgeID = 0; edgeID < _vertexCount - 1; ++edgeID) { int firstVertexID = FastIO.ReadNonNegativeInt() - 1; int secondVertexID = FastIO.ReadNonNegativeInt() - 1; int edgeWeight = FastIO.ReadNonNegativeInt(); _verticesNeighbors[firstVertexID].Add(secondVertexID); _verticesNeighbors[secondVertexID].Add(firstVertexID); _verticesEdgeWeights[firstVertexID].Add(edgeWeight); _verticesEdgeWeights[secondVertexID].Add(edgeWeight); _verticesEdges[firstVertexID].Add(edgeID); _verticesEdges[secondVertexID].Add(edgeID); } BuildRootedStructure( parentVertexID: -1, vertexID: 0, depth: 0); _chainIndex = 0; _baseArrayIndex = 0; RunHLD( parentVertexID: -1, vertexID: 0, edgeWeight: 0); BuildAncestorTable(); BuildSegmentTree( segmentTreeIndex: 0, segmentStartIndex: 0, segmentEndIndex: _vertexCount - 1); char instruction; while ((instruction = FastIO.ReadInstruction()) != 'D') { if (instruction == 'Q') { FastIO.WriteNonNegativeInt(Query( firstVertexID: FastIO.ReadNonNegativeInt() - 1, secondVertexID: FastIO.ReadNonNegativeInt() - 1)); FastIO.WriteLine(); } else { Change( edgeID: FastIO.ReadNonNegativeInt() - 1, weight: FastIO.ReadNonNegativeInt()); } } } FastIO.Flush(); }