Special Edge
Practice
4 (2 votes)
C++
Algorithms
Graphs
Graph representation
Problem
70% Success 1140 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
Given a connected, undirected graph \(G\) with \(N\) nodes and \(M\) edges. Every node has a value assigned to it, denoted by the array \(A\).
An edge \(E\) is said to be special iff :
- There exists at least one pair \((u, v), u < v\) such that any simple path between node \(u\) and \(v\) will always include the edge \(E\).
- The strength of such an edge is defined as \(\sum A[u] \times A[v]\) over all such pairs.
Among all the special edges, find the special edge with the maximum strength in the graph.
If there exists more than one answer, suppose edge \(E (a, b)\) and \(E'(a',b')\) where \(a < b \ \& \ a' < b'\)
- Return the edge with smaller node as the first value in the pair i.e. \(a, a'\)
- If, there still exists more than one answer, return the edge with smaller node as the second value in the pair i.e. \(b, b'\)
If there does not exists any such edge, return pair \((n+1, n+1)\)
Input format
- First line contains two space-separated integers \(N, M\)
- Next line contains \(N\) space-separated integers denoting the array A.
- Next \(M\) lines contains two space-separated integers denoting an edge.
Output format
Print two space-separated integers \(a \ b\), denoting the special edge with maximum strength. Please note \(a < b\)
Constraints
\(1 \le N, M\le 10^5 \\ 1 \le A[i] \le 10^4\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
72 votes
Tags:
Medium
Points:30
10 votes
Tags:
AlgorithmsBreadth First SearchDepth First SearchGraphsMediumSetsTrees
Points:30
4 votes
Tags:
Graph RepresentationAlgorithmsGraphsC++
Editorial