Knowee
Questions
Features
Study Tools

Satya Prakash has some 'N' amount of blank cards of heart-shaped. Each of the other cards is hanging on to exactly one other card by a piece of string, whereas Card 1 is affixed to the wall directly. Specifically, card i (i>1) is hanging onto card p[i] , p[i] is smaller than i.During the Initial time, Satya Prakash must write one integer number on each card. He does this by choosing any permutation a of [1,2,…,n]. Then, the number written on card i is a[i].After that, Satya Prakash must do the following operation n times while maintaining a sequence s (which is initially empty):Choose a card x such that no other cards are hanging onto it.Append the number written on card x to the end of s.If x≠1 and the number on card px is larger than the number on card x, replace the number on card px with the number on card x.Remove card x.After that, Satya Prakash will have a sequence s with n elements. What is the maximum length of the longest non-decreasing subsequence† of s at the end if Satya Prakash does all the steps optimally?Input FormatThe first line contains a single integer n — the number of cards.The second line contains n−1 integers p2,p3,…,pn describing which card that each card hangs onto.Constraints2≤n≤10^51 ≤ p[i] < iOutput FormatPrint a single integer — the maximum length of the longest non-decreasing subsequence of s at the end if Satya Prakash does all the steps optimally.Sample Input 081 1 1 2 3 1 4Sample Output 07Sample Input 171 2 2 4 5 1Sample Output 15

Question

Satya Prakash has some 'N' amount of blank cards of heart-shaped. Each of the other cards is hanging on to exactly one other card by a piece of string, whereas Card 1 is affixed to the wall directly. Specifically, card i (i>1) is hanging onto card p[i] , p[i] is smaller than i.During the Initial time, Satya Prakash must write one integer number on each card. He does this by choosing any permutation a of [1,2,…,n]. Then, the number written on card i is a[i].After that, Satya Prakash must do the following operation n times while maintaining a sequence s (which is initially empty):Choose a card x such that no other cards are hanging onto it.Append the number written on card x to the end of s.If x≠1 and the number on card px is larger than the number on card x, replace the number on card px with the number on card x.Remove card x.After that, Satya Prakash will have a sequence s with n elements. What is the maximum length of the longest non-decreasing subsequence† of s at the end if Satya Prakash does all the steps optimally?Input FormatThe first line contains a single integer n — the number of cards.The second line contains n−1 integers p2,p3,…,pn describing which card that each card hangs onto.Constraints2≤n≤10^51 ≤ p[i] < iOutput FormatPrint a single integer — the maximum length of the longest non-decreasing subsequence of s at the end if Satya Prakash does all the steps optimally.Sample Input 081 1 1 2 3 1 4Sample Output 07Sample Input 171 2 2 4 5 1Sample Output 15

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is a graph theory problem. Here's a step-by-step solution:

Step 1: Understand the problem Satya Prakash has 'N' heart-shaped cards, each card is hanging onto exactly one other card by a string. Card 1 is affixed to the wall directly. Satya Prakash must write one integer number on each card by choosing any permutation a of [1,2,…,n]. Then, he must perform a series of operations on the cards. The goal is to find the maximum length of the longest non-decreasing subsequence of the sequence s at the end if Satya Prakash does all the steps optimally.

Step 2: Identify the input and output The input is a single integer n — the number of cards, and n−1 integers p2,p3,…,pn describing which card that each card hangs onto. The output is a single integer — the maximum length of the longest non-decreasing subsequence of s at the end.

Step 3: Formulate a strategy The problem can be solved by using depth-first search (DFS) on the graph. The idea is to find the longest path in the graph, which corresponds to the longest non-decreasing subsequence in the sequence s.

Step 4: Implement the strategy First, construct the graph based on the input. Then, perform DFS on the graph to find the longest path. The length of the longest path is the maximum length of the longest non-decreasing subsequence of s.

Step 5: Test the solution Test the solution with the provided sample inputs and outputs to ensure it works correctly.

Please note that this problem requires a good understanding of graph theory and depth-first search. If you're not familiar with these concepts, you might need to study them first before attempting to solve this problem.

This problem has been solved

Similar Questions

Sita has a collection of cards represented as an array. She wishes to add a new card to her collection, specifically placing it in the second position within this array. Develop a program that will enable Sita to insert the new card at the second position (The value of the position starts from 1).Input format :The first line of input is an integer n as input, representing the number of cards in Sita's collection.The second line of input consists of the n space-separated integers cards[i], representing the values of each card in her collection.The third line of input is an additional integer, insertValue, representing the value of the new card to be inserted at the second position.Output format :The output displays a single line of n+1 space-separated integers in the updated collection of cards.

Hari takes a card from a deck which has got only two-digit numbers. The card he picked has got a number which can be obtained either by multiplying the digit sum by 8 and adding 1 to it or by multiplying the difference of digits by 13 and adding 2 to it. The number Hari got is ____.Marks : 2Negative Marks : 0Answer here54522541

You have some cards. An integer between 11 and n𝑛 is written on each card: specifically, for each i𝑖 from 11 to n𝑛, you have ai𝑎𝑖 cards which have the number i𝑖 written on them.There is also a shop which contains unlimited cards of each type. You have k𝑘 coins, so you can buy k𝑘 new cards in total, and the cards you buy can contain any integer between 11 and n𝑛.After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length n𝑛 which are a permutation of [1,2,…,n][1,2,…,𝑛]. What's the maximum score you can get?InputEach test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100)𝑡 (1≤𝑡≤100). The description of the test cases follows.The first line of each test case contains two integers n𝑛, k𝑘 (1≤n≤2⋅1051≤𝑛≤2⋅105, 0≤k≤10120≤𝑘≤1012) — the number of distinct types of cards and the number of coins.The second line of each test case contains n𝑛 integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛 (1≤ai≤10121≤𝑎𝑖≤1012) — the number of cards of type i𝑖 you have at the beginning.It is guaranteed that the sum of n𝑛 over all test cases does not exceed 5⋅1055⋅105.OutputFor each test case, output a single line containing an integer: the maximum score you can get.ExampleinputCopy81 1012 48 43 46 1 83 97 6 25 36 6 7 4 69 77 6 1 7 6 2 4 3 310 101 3 1 2 1 9 3 5 7 59 85 8 7 5 1 3 2 9 8outputCopy1115152228322836

Input FormatThe first and only line of input contains an integer N.Output FormatPrint the count of permutations.

Three friends, Syam, Samanta and Siri have a set of 6 algebraic cards as shown below, on a table. Each card has an algebraic expression written on it. Here, ‘x’ is an integer. Each person has to pick up two cards, alternatively

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.