Approach hintTry it first, then revealHide
Sort the edges by weight and answer the queries offline in increasing order of the limit. A merge of components with sizes a and b creates a * b new valid pairs; keep the total in 64 bits.
Sort the edges by weight and answer the queries offline in increasing order of the limit. A merge of components with sizes a and b creates a * b new valid pairs; keep the total in 64 bits.