Balance Parenthesis Again

2

1 votes
Algorithms, Breadth-first search, Graphs
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Given a set of N nodes, where each node has a character A[i] associated with it. A[i] can be either an opening bracket ( or a closing bracket ). Construct a tree using N nodes numbered 1 to N such that the value of the given function Z is maximized.

Z=i=Ni=1[j=Nj=i+1[F(i,j)]], where F(i,j) = Length of the longest balanced parenthesis substring on the simple path between node i and node j.

A string is said to be balanced if

  • Empty string is a balanced string.
  • if s is a balanced string, then (s) is also a balanced string.
  • if s and t are balanced strings, then st (concatenation of s and t) is also a balanced string.

For example : (())) is not balanced as last closing bracket is unmatched.

Input format

  • The first line contains an integer denoting the number of nodes in the tree.
  • The next line contains a string A of length N where each character denotes the value of each node i.e. A[i].

Output format

  • Print N - 1 lines where each line contains
    • Two space-separated integers a, b denote an edge between nodes a and b in the tree. 

Constraints

2N500

Verdict and Scoring

  • is the score for each test case.
  • If the output is not a tree or contains any invalid entry, then you will receive a Wrong Answer verdict.
Sample Input
3 
(()
Sample Output
1 2
2 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Value of F(i,j) are as follows:

  • F(1,2)=((=0
  • F(1,3)=(()=2
  • F(2,3)=()=2

Hence,  Z = 2 + 2 = 4.

Editor Image

?