Identify the Hub of a Guaranteed Star. edges describes a valid undirected star on vertices labeled 1..n: one center is connected to every other vertex, with no other edges. Return the center vertex label.
### Problem
`edges` describes a valid undirected star on vertices labeled `1..n`: one center is connected to every other vertex, with no other edges. Return the center vertex label.
```cpp
class Solution {
public:
int findCenter(vector<vector<int>>& edges);
};
```
### Examples
**Example 1:**
```text
edges = [[3,1],[2,3]]
Output: 3
Explanation: Vertex 3 is shared by both edges.
```
**Example 2:**
```text
edges = [[4,2],[4,1],[3,4],[5,4]]
Output: 4
Explanation: Every edge has endpoint 4.
```
### Constraints
- `3 <= n <= 10^5` and `edges.length == n-1`.
- Every edge has two distinct endpoints in `[1,n]`.
- The input is guaranteed to be a valid star.
### Clarifying Questions
The validity guarantee means general graph-center computation and validation are unnecessary. At least two edges exist.
```hint Inspect the structure
Which facts follow from the guaranteed star, and which label is the requested output?
```
### Reasoning checkpoints
- Use only consequences justified by the guaranteed-star input.
- Show that the returned label is the unique center, regardless of endpoint or edge order.
### Optional extensions
If the star guarantee were removed, how would you check for a universal vertex?
Overview: Identify the Hub of a Guaranteed Star. edges describes a valid undirected star on vertices labeled 1..n: one center is connected to every other vertex, with no other edges. Return the center vertex label.
edges describes a valid undirected star on vertices labeled 1..n: one center is connected to every other vertex, with no other edges. Return the center vertex label.
class Solution {
public:
int findCenter(vector<vector<int>>& edges);
};
Examples
Example 1:
edges = [[3,1],[2,3]]
Output: 3
Explanation: Vertex 3 is shared by both edges.
Example 2:
edges = [[4,2],[4,1],[3,4],[5,4]]
Output: 4
Explanation: Every edge has endpoint 4.
Constraints
3 <= n <= 10^5
and
edges.length == n-1
.
Every edge has two distinct endpoints in
[1,n]
.
The input is guaranteed to be a valid star.
Clarifying Questions Guidance
The validity guarantee means general graph-center computation and validation are unnecessary. At least two edges exist.
Reasoning checkpoints
Use only consequences justified by the guaranteed-star input.
Show that the returned label is the unique center, regardless of endpoint or edge order.
Optional extensions
If the star guarantee were removed, how would you check for a universal vertex?