title: Calculate the number of nodes on either side of an edge in a tree
body: <p>A tree here means an acyclic undirected graph with <strong>n</strong> nodes and <strong>n-1</strong> edges. For each edge in the tree, calculate the number of nodes on either side of it. If on removing the edge, you get two trees having <strong>a</strong> and <strong>b</strong> number of nodes, then I want to find those values <strong>a</strong> and <strong>b</strong> for all edges in the tree (ideally in O(n) time).</p>
  
  <p>Intuitively I feel a multisource BFS starting from all the "leaf" nodes would yield an answer, but I'm not able to translate it into code.</p>
  
  <p>For extra credit, provide an algorithm that works in any general graph.</p>