|
| 1 | +# [2924. Find Champion II](https://leetcode.com/problems/find-champion-ii) |
| 2 | + |
| 3 | +[中文文档](/solution/2900-2999/2924.Find%20Champion%20II/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>There are <code>n</code> teams numbered from <code>0</code> to <code>n - 1</code> in a tournament; each team is also a node in a <strong>DAG</strong>.</p> |
| 8 | + |
| 9 | +<p>You are given the integer <code>n</code> and a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code><font face="monospace">m</font></code> representing the <strong>DAG</strong>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a directed edge from team <code>u<sub>i</sub></code> to team <code>v<sub>i</sub></code> in the graph.</p> |
| 10 | + |
| 11 | +<p>A directed edge from <code>a</code> to <code>b</code> in the graph means that team <code>a</code> is <strong>stronger</strong> than team <code>b</code> and team <code>b</code> is <strong>weaker</strong> than team <code>a</code>.</p> |
| 12 | + |
| 13 | +<p>Team <code>a</code> will be the <strong>champion</strong> of the tournament if there is no team <code>b</code> that is <strong>stronger</strong> than team <code>a</code>.</p> |
| 14 | + |
| 15 | +<p>Return <em>the team that will be the <strong>champion</strong> of the tournament if there is a <strong>unique</strong> champion, otherwise, return </em><code>-1</code><em>.</em></p> |
| 16 | + |
| 17 | +<p><strong>Notes</strong></p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>A <strong>cycle</strong> is a series of nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, a<sub>n+1</sub></code> such that node <code>a<sub>1</sub></code> is the same node as node <code>a<sub>n+1</sub></code>, the nodes <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> are distinct, and there is a directed edge from the node <code>a<sub>i</sub></code> to node <code>a<sub>i+1</sub></code> for every <code>i</code> in the range <code>[1, n]</code>.</li> |
| 21 | + <li>A <strong>DAG</strong> is a directed graph that does not have any <strong>cycle</strong>.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong class="example">Example 1:</strong></p> |
| 26 | + |
| 27 | +<p><img height="300" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2924.Find%20Champion%20II/images/graph-3.png" width="300" /></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> n = 3, edges = [[0,1],[1,2]] |
| 31 | +<strong>Output:</strong> 0 |
| 32 | +<strong>Explanation: </strong>Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<p><img height="300" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2900-2999/2924.Find%20Champion%20II/images/graph-4.png" width="300" /></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> n = 4, edges = [[0,2],[1,3],[1,2]] |
| 41 | +<strong>Output:</strong> -1 |
| 42 | +<strong>Explanation:</strong> Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= n <= 100</code></li> |
| 50 | + <li><code>m == edges.length</code></li> |
| 51 | + <li><code>0 <= m <= n * (n - 1) / 2</code></li> |
| 52 | + <li><code>edges[i].length == 2</code></li> |
| 53 | + <li><code>0 <= edge[i][j] <= n - 1</code></li> |
| 54 | + <li><code>edges[i][0] != edges[i][1]</code></li> |
| 55 | + <li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code>, team <code>b</code> is not stronger than team <code>a</code>.</li> |
| 56 | + <li>The input is generated such that if team <code>a</code> is stronger than team <code>b</code> and team <code>b</code> is stronger than team <code>c</code>, then team <code>a</code> is stronger than team <code>c</code>.</li> |
| 57 | +</ul> |
| 58 | + |
| 59 | +## Solutions |
| 60 | + |
| 61 | +<!-- tabs:start --> |
| 62 | + |
| 63 | +### **Python3** |
| 64 | + |
| 65 | +```python |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +### **Java** |
| 70 | + |
| 71 | +```java |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### **C++** |
| 76 | + |
| 77 | +```cpp |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +### **Go** |
| 82 | + |
| 83 | +```go |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +### **...** |
| 88 | + |
| 89 | +``` |
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | +<!-- tabs:end --> |
0 commit comments