Count Tree Vertices with Pythagorean Distances
Company: IBM
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Given an unweighted, undirected tree with `n` vertices labeled `0` through `n - 1`, and three distinct fixed vertices `x`, `y`, and `z`, count the vertices whose distances to those three fixed vertices form a positive Pythagorean triple.
For each vertex `v`, sort its three distances into `a <= b <= c`. Count `v` exactly once if `a > 0` and `a * a + b * b == c * c`. A distance is the number of edges on the unique path between two vertices.
### Input
- `n`: the number of vertices.
- `edges`: `n - 1` pairs of vertex labels describing a connected tree.
- `x`, `y`, `z`: three distinct vertex labels in the tree.
### Output
Return the number of qualifying vertices as an integer.
### Constraints and Edge Cases
- For this practice version, `3 <= n <= 100000`.
- Treat edges as unweighted and the three fixed vertices as distinct in this practice version.
- Every edge joins two different vertices. There are no duplicate edges.
- The tree may be a chain; do not assume its depth is small.
- The three distances have no required association with `x`, `y`, and `z` after sorting.
- A vertex at distance zero from a fixed vertex cannot qualify, even if its squared distances otherwise satisfy the equation.
- Square distances using arithmetic that can represent values up to `(n - 1) * (n - 1)` exactly.
### Example 1
```text
n = 12
edges = [[0,1],[1,2],[2,3],[0,4],[4,5],[5,6],[6,7],[0,8],[8,9],[9,10],[10,11]]
x = 3
y = 7
z = 11
output = 2
```
Vertices `4` and `8` each have sorted distances `[3, 4, 5]`. No other vertex qualifies.
### Example 2
```text
n = 3
edges = [[0,1],[1,2]]
x = 0
y = 1
z = 2
output = 0
```
Every vertex is one of the fixed vertices and therefore has a zero distance.
Overview: Count tree vertices whose positive distances to three fixed vertices form a Pythagorean triple, including distance ordering and large-tree edge cases.
Read the full IBM Software Engineer interview experience this question came from
Given an unweighted, undirected tree with `n` vertices labeled `0` through `n - 1`, and three distinct fixed vertices `x`, `y`, and `z`, count the vertices whose distances to those three fixed vertices form a positive Pythagorean triple.
For each vertex `v`, sort its three distances into `a <= b <= c`. Count `v` exactly once if `a > 0` and `a * a + b * b == c * c`. A distance is the number of edges on the unique path between two vertices.
### Input
- `n`: the number of vertices.
- `edges`: `n - 1` pairs of vertex labels describing a connected tree.
- `x`, `y`, `z`: three distinct vertex labels in the tree.
### Output
Return the number of qualifying vertices as an integer.
### Constraints and Edge Cases
- For this practice version, `3 <= n <= 100000`.
- Treat edges as unweighted and the three fixed vertices as distinct in this practice version.
- Every edge joins two different vertices. There are no duplicate edges.
- The tree may be a chain; do not assume its depth is small.
- The three distances have no required association with `x`, `y`, and `z` after sorting.
- A vertex at distance zero from a fixed vertex cannot qualify, even if its squared distances otherwise satisfy the equation.
- Square distances using arithmetic that can represent values up to `(n - 1) * (n - 1)` exactly.
### Example 1
```text
n = 12
edges = [[0,1],[1,2],[2,3],[0,4],[4,5],[5,6],[6,7],[0,8],[8,9],[9,10],[10,11]]
x = 3
y = 7
z = 11
output = 2
```
Vertices `4` and `8` each have sorted distances `[3, 4, 5]`. No other vertex qualifies.
### Example 2
```text
n = 3
edges = [[0,1],[1,2]]
x = 0
y = 1
z = 2
output = 0
```
Every vertex is one of the fixed vertices and therefore has a zero distance.
Constraints
- 3 <= n <= 100000; edges contains n-1 distinct undirected edges and forms a connected unweighted tree on labels 0 through n-1.
- x, y and z are distinct vertex labels. A distance is an edge count on the unique path.
- For each vertex, sort its three distances as a <= b <= c and count it once exactly when a > 0 and a*a+b*b == c*c.
- Positive scaled Pythagorean triples qualify; zero-distance vertices do not.
- The tree may have depth n-1. Individual squares can reach 9999800001, and the sum of two squares is at most 19999600002.
Examples
Input: (12, [[0, 1], [1, 2], [2, 3], [0, 4], [4, 5], [5, 6], [6, 7], [0, 8], [8, 9], [9, 10], [10, 11]], 3, 7, 11)
Expected Output: 2
Explanation: Published sample 1: vertices 4 and 8 have [3,4,5]; all other vertices fail.
Input: (3, [[0, 1], [1, 2]], 0, 1, 2)
Expected Output: 0
Explanation: Published sample 2: every vertex is fixed and has a zero distance.