Tap here to switch tabs
Problem
Submissions

Problem: Pythagorean Distance Nodes in a Tree

Try to solve the Pythagorean Distance Nodes in a Tree problem.

Statement

You are given an unweighted undirected tree with n nodes labeled from 00 to n −1- 1, represented by a 22D integer array edges of length n −1- 1, where edges[i] =[ui,vi]= [u_i, v_i] indicates an edge between nodes uiu_i and viv_i.

You are also given three pairwise distinct nodes x, y, and z.

For any node u in the tree, let d1d_1, d2d_2, and d3d_3 be the distances from u to nodes x, y, and z sorted in nondecreasing order such that d1≤d2≤d3d_1 \leq d_2 \leq d_3. A node u is called special if these distances satisfy the Pythagorean condition:

Return the number of special nodes in the tree.

Constraints:

  • 4≤n≤1054 \leq n \leq 10^5

  • edges.length=n−1\text{edges.length} = n - 1

  • edges[i]=[ui,vi]\text{edges}[i] = [u_i, v_i]

  • 0≤ui,vi,x,y,z≤n−10 \leq u_i, v_i, x, y, z \leq n - 1

  • x, y, and z are pairwise distinct.

  • The input is generated such that edges represents a valid tree.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Pythagorean Distance Nodes in a Tree

Try to solve the Pythagorean Distance Nodes in a Tree problem.

Statement

You are given an unweighted undirected tree with n nodes labeled from 00 to n −1- 1, represented by a 22D integer array edges of length n −1- 1, where edges[i] =[ui,vi]= [u_i, v_i] indicates an edge between nodes uiu_i and viv_i.

You are also given three pairwise distinct nodes x, y, and z.

For any node u in the tree, let d1d_1, d2d_2, and d3d_3 be the distances from u to nodes x, y, and z sorted in nondecreasing order such that d1≤d2≤d3d_1 \leq d_2 \leq d_3. A node u is called special if these distances satisfy the Pythagorean condition:

Return the number of special nodes in the tree.

Constraints:

  • 4≤n≤1054 \leq n \leq 10^5

  • edges.length=n−1\text{edges.length} = n - 1

  • edges[i]=[ui,vi]\text{edges}[i] = [u_i, v_i]

  • 0≤ui,vi,x,y,z≤n−10 \leq u_i, v_i, x, y, z \leq n - 1

  • x, y, and z are pairwise distinct.

  • The input is generated such that edges represents a valid tree.