Connecting roads
Practice
2.6 (22 votes)
Heavy light decomposition
Advanced algorithms
Algorithms
Problem
25% Success 3693 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code
Your country has \(N\) cities and \(N-1\) roads connecting pairs of cities in such a way that there is a path between any pair of cities. For each road, you are given its toll that is the amount of money one has to pay in order to traverse it. The cost of traveling from city \(A\) to city \(B\) is the sum of the tolls of the roads in the path from \(A\) to \(B\).
You are given \(Q\) queries to process. There are two types of queries:
- \(0\ c\): Due to inflation, the toll of all roads adjacent to city \(c\) (all roads that have \(c\) as one of its endpoints) is multiplied by 2.
- \(1\ x\ y\): You are required to find the cost of traveling from \(x\) to \(y\).
Since the answers can be very large, print them modulo \(1,000,000,007\).
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
AlgorithmsOpenApprovedHardAlgorithms
2.Upgrade
Points:50
24 votes
Tags:
Data StructuresHardOpenBinary search treeHeavy light decompositionAlgorithms
Points:50
Tags:
Dynamic ProgrammingHardHeavy light decompositionAlgorithmsOpenApprovedAlgorithmsDisjoint set
Editorial