CF - Sloth Naptime
Author: Sofia Yang
Announcement With Official Editorial
Explanation
Time Complexity:
Basically the problem statement asks if given a path from a start node to an end node on a tree, how far the sloth will travel if it has a set amount of energy, and each edge on the path has an energy cost.
We know that the sloth will always try to move upwards from the start node to the least common ancestor of the start and end nodes , and then move downwards from the least common ancestor towards the end node .
Let be the distance from node to node .
So if the sloth has energy amount , there are 3 separate cases:
. In this case the result will be the sloth reaching the end.
and in which the result will be the sloth reaching the th parent of .
and in which the result will be the sloth reaching the th parent of .
So first, we can can run DFS once to find the depth of every node.
Then we can create a matrix to store the ancestors of each node, where is the th ancestor of node .
After that, we can use binary lifting to answer each query by finding the least common ancestor of the start and end nodes, and then finding the right node that the sloth will reach.
Implementation
Java
import java.io.*;import java.util.*;public class SlothNaptime {public static final int MAXN = (int)3e5 + 5;public static final int LOGN = (int)(Math.log(MAXN) / Math.log(2)) + 1;public static ArrayList<Integer>[] adj = new ArrayList[MAXN];// anc[i][j] is the 2^j-th parent of i.public static int[][] anc = new int[MAXN][LOGN];public static int[] depth = new int[MAXN];
C++
#include <bits/stdc++.h>using namespace std;const int MAXN = 3e5 + 5;const int LOGN = log2(MAXN) + 1;int depth[MAXN];// anc[i][j] is the 2^j-th parent of i.int anc[MAXN][LOGN];vector<int> adj[MAXN];
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!