Class Solution
- java.lang.Object
-
- g1701_1800.s1782_count_pairs_of_nodes.Solution
-
public class Solution extends Object
1782 - Count Pairs Of Nodes.Hard
You are given an undirected graph defined by an integer
n, the number of nodes, and a 2D integer arrayedges, the edges in the graph, whereedges[i] = [ui, vi]indicates that there is an undirected edge betweenuiandvi. You are also given an integer arrayqueries.Let
incident(a, b)be defined as the number of edges that are connected to either nodeaorb.The answer to the
jthquery is the number of pairs of nodes(a, b)that satisfy both of the following conditions:a < bincident(a, b) > queries[j]
Return an array
answerssuch thatanswers.length == queries.lengthandanswers[j]is the answer of thejthquery.Note that there can be multiple edges between the same two nodes.
Example 1:

Input: n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]
Output: [6,5]
Explanation: The calculations for incident(a, b) are shown in the table above. The answers for each of the queries are as follows:
-
answers[0] = 6. All the pairs have an incident(a, b) value greater than 2.
-
answers[1] = 5. All the pairs except (3, 4) have an incident(a, b) value greater than 3.
Example 2:
Input: n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]
Output: [10,10,9,8,6]
Constraints:
2 <= n <= 2 * 1041 <= edges.length <= 1051 <= ui, vi <= nui != vi1 <= queries.length <= 200 <= queries[j] < edges.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]countPairs(int n, int[][] edges, int[] queries)
-