To solve this problem, we'll use Heavy-Light Decomposition (HLD) combined with a Segment Tree. This approach allows us to efficiently handle range queries and updates on paths within a tree, which is crucial given the problem's constraints.
Problem Overview
- Tree Structure: A rooted tree with
n nodes, where node 1 is the root. All starting positions p_i of children are leaf nodes.
- Children: Each child starts at a unique leaf node with a specific speed
v_i.
- Movement: Children move towards the root at their respective speeds. If two or more children meet at any point:
- If a child has a lower speed than another, it gets stopped.
- If multiple children have the same speed, all can continue unaffected.
- Goal: Determine which children successfully reach the root.
Algorithm Overview
-
Tree Representation:
- Use an adjacency list to represent the tree.
- Perform a Depth-First Search (DFS) to compute sizes, depths, parents, and identify heavy children.
-
Heavy-Light Decomposition (HLD):
- Decompose the tree into chains to facilitate efficient path queries and updates.
- Assign each node a position in a linear array which will be used by the Segment Tree.
-
Segment Tree:
- Implement a Segment Tree that supports range maximum queries and range maximum updates.
- This allows us to keep track of the maximum speed along any path from a node to the root.
-
Processing Children:
- Sort the children in descending order of speed.
- For children with the same speed, process them together.
- For each child:
- Query the maximum speed along the path from its starting node to the root.
- If the child's speed is greater than the queried maximum, it can reach the root.
- Update the path with the child's speed to block slower children.
-
Output:
- Collect and output the indices of children who can reach the root in ascending order.
C++ Implementation
Below is the C++ implementation of the above approach. This code uses HLD and a custom Segment Tree to efficiently determine which children reach the root.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Segment Tree with range max updates and range max queries
struct SegmentTree {
int n;
vector<ll> tree, lazy;
SegmentTree(int size) : n(size), tree(4*size, 0), lazy(4*size, 0) {}
// Push down the lazy updates
void push(int node, int start, int end) {
if(lazy[node] != 0){
tree[node] = max(tree[node], lazy[node]);
if(start != end){
lazy[node*2] = max(lazy[node*2], lazy[node]);
lazy[node*2+1] = max(lazy[node*2+1], lazy[node]);
}
lazy[node] = 0;
}
}
// Update the range [l, r] with value val (max operation)
void update_range(int node, int start, int end, int l, int r, ll val){
push(node, start, end);
if(r < start || l > end)
return;
if(l <= start && end <= r){
lazy[node] = max(lazy[node], val);
push(node, start, end);
return;
}
int mid = (start + end) /2;
update_range(node*2, start, mid, l, r, val);
update_range(node*2+1, mid+1, end, l, r, val);
tree[node] = max(tree[node*2], tree[node*2+1]);
}
// Query the maximum in range [l, r]
ll query_max(int node, int start, int end, int l, int r){
push(node, start, end);
if(r < start || l > end)
return LLONG_MIN;
if(l <= start && end <= r)
return tree[node];
int mid = (start + end)/2;
return max(query_max(node*2, start, mid, l, r),
query_max(node*2+1, mid+1, end, l, r));
}
};
// Heavy-Light Decomposition
struct HLD {
int n;
vector<vector<int>> adj;
vector<int> parent, depth, heavy, head, pos;
int cur_pos;
HLD(int size) : n(size), adj(size+1), parent(size+1, 0), depth(size+1, 0),
heavy(size+1, -1), head(size+1, 0), pos(size+1, 0), cur_pos(1) {}
// Add edge
void add_edge(int u, int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
// DFS to compute sizes and heavy child
int dfs(int u, int p){
parent[u] = p;
int size =1;
int max_size =0;
for(auto &v : adj[u]){
if(v != p){
depth[v] = depth[u] +1;
int sz = dfs(v, u);
size += sz;
if(sz > max_size){
max_size = sz;
heavy[u] = v;
}
}
}
return size;
}
// Decompose the tree
void decompose_tree(int u, int h){
head[u] = h;
pos[u] = cur_pos++;
if(heavy[u] != -1)
decompose_tree(heavy[u], h);
for(auto &v : adj[u]){
if(v != parent[u] && v != heavy[u]){
decompose_tree(v, v);
}
}
}
// Initialize HLD
void init(){
dfs(1, -1);
decompose_tree(1, 1);
}
// Query maximum on path from u to v
ll query_path(SegmentTree &st, int u, int v){
ll res = LLONG_MIN;
while(head[u] != head[v]){
if(depth[head[u]] < depth[head[v]])
swap(u, v);
res = max(res, st.query_max(1, 1, n, pos[head[u]], pos[u]));
u = parent[head[u]];
}
if(depth[u] > depth[v])
swap(u, v);
res = max(res, st.query_max(1, 1, n, pos[u], pos[v]));
return res;
}
// Update path from u to v with value val
void update_path(SegmentTree &st, int u, int v, ll val){
while(head[u] != head[v]){
if(depth[head[u]] < depth[head[v]])
swap(u, v);
st.update_range(1, 1, n, pos[head[u]], pos[u], val);
u = parent[head[u]];
}
if(depth[u] > depth[v])
swap(u, v);
st.update_range(1, 1, n, pos[u], pos[v], val);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<ll> child_speed(m);
for(int i=0;i<m;i++) cin >> child_speed[i];
vector<int> p_i(m);
for(int i=0;i<m;i++) cin >> p_i[i];
HLD hld(n);
// Read edges
for(int i=0;i<n-1;i++){
int u, v, w;
cin >> u >> v >> w;
hld.add_edge(u, v);
}
hld.init();
// Create list of children with their speed and position
struct Child {
int idx;
ll speed;
int posi;
};
vector<Child> children(m);
for(int i=0;i<m;i++){
children[i].idx = i+1;
children[i].speed = child_speed[i];
children[i].posi = p_i[i];
}
// Sort children by decreasing speed
sort(children.begin(), children.end(), [&](const Child &a, const Child &b) -> bool{
if(a.speed != b.speed) return a.speed > b.speed;
return a.idx < b.idx;
});
SegmentTree st(n);
// To store which children can reach
vector<bool> can_reach(m, false);
int i=0;
while(i < m){
ll current_speed = children[i].speed;
int j = i;
// Find all children with the same speed
while(j < m && children[j].speed == current_speed) j++;
// First, check which can reach
for(int k=i; k<j; k++){
ll max_speed = hld.query_path(st, children[k].posi, 1);
if(max_speed < current_speed){
can_reach[children[k].idx -1] = true;
}
}
// Then, update the paths
for(int k=i; k<j; k++){
hld.update_path(st, children[k].posi, 1, children[k].speed);
}
i = j;
}
// Collect and sort the result
vector<int> result;
for(int i=0;i<m;i++) if(can_reach[i]) result.push_back(i+1);
cout << result.size() << "\n";
for(int i=0;i<result.size();i++) cout << result[i] << (i < result.size()-1 ? ' ':'\n');
}
Explanation of the Code
-
Data Structures:
- SegmentTree: Handles range maximum queries and updates using lazy propagation.
- HLD: Manages the Heavy-Light Decomposition of the tree, enabling efficient path queries and updates.
-
Reading Input:
- Number of nodes
n and number of children m.
- Speeds of the children and their starting positions.
- The tree structure is built by reading
n-1 edges.
-
Heavy-Light Decomposition:
- The
HLD structure is initialized and decomposed to assign each node to a chain with associated head and position.
-
Processing Children:
- Children are sorted in descending order of speed.
- For each group of children with the same speed:
- Check Reachability: Query the maximum speed along the path from the child's starting node to the root. If this maximum is less than the child's speed, it can reach the root.
- Update Paths: After determining reachability, update the paths to reflect the child's speed, preventing slower children from proceeding along these paths.
-
Output:
- After processing all children, collect and print the indices of those who can reach the root in ascending order.
Sample Runs
Sample Input 1:
4 2
3 4
3 4
1 2 2
2 3 3
2 4 4
Sample Output 1:
1
2
Sample Input 2:
4 2
3 3
3 4
1 2 2
2 3 3
2 4 4
Sample Output 2:
2
1 2
Conclusion
This solution efficiently determines which children can reach the root by leveraging Heavy-Light Decomposition and a Segment Tree to handle range queries and updates. The approach ensures that even with large input sizes (n, m ≤ 100,000), the solution remains performant.