Programming Geek
Rated 4.1/5 based on 446 reviews

### Question : Employee Hierarchy

Adam is working in a large hierarchical organization, where every employee, except the CEO, is either a supervisor or a sub-ordinate or both. Every employee will be identified by Employee ID. CEO of the company has 1 as his employee id. Every employee will get the salary (S) in accordance with their level and their performance. Given the data about the all the above mentioned details, your task is to find out the number of sub-ordinates with salary less than Adam.

Note:

1. If an employee appears at two different levels, then consider the level which is near (best) to the CEO.
2. An employee is considered as sub-ordinate, if Adam is a direct or indirect supervisor to the said employee.

Input Format:

Input consists of three parts, viz.
1. First Line contains, number of employees (N), number of supervisor-subordinate relationships (M) and Adam's employee id (A)
2. Next M lines follow, each line contains two employee Ids I, J which represents that I is a supervisor of J.
3. Salaries of N employees, delimited by space

Output Format:

Print the total number of employees who are sub-ordinates to Adam and have salary less than Adam's.

Constraints:
1<=N<=50
1<=M<=1000
1<=I, J<=N
1<=S<=50000

Sample Input and Output

SNo.InputOutput
1
5 5 2
1 2
2 3
2 4
4 5
5 3
1500 2000 1500 2500 1800

2

K friends came to the city and they are in search of some nice flats to stay. At most they can spend T Rupees for the accommodation. Finally they came to know about Marika Apartments, where there exist N empty flats, and every flat owner is allowing at-most Pj persons to stay in a jth flat. So finally they have decided to stay in the multiple flats, at the same time they would like to stay in such flats in which every person from the K person group should able to reach the other K-1 friends who are actually staying either in the same flat or in the other flat.

Let Rj define the rent of the jth flat. One of the major problem in the apartment is only few flats are reachable from the other and one can take the advantage of one flat to reach the other in an indirect way. Now having all the above information your task is to help K friends to know how many options are available for them to stay.

Input Format:

Input consists of four parts, viz.
First Line contains, number of friends (K), number of flats (N) and total paying capacity (T)
Next N lines contain N spaced integers, which represents the connectivity between the N flats. The connectivity between the flats are directional in nature and if Jth flat is reachable from Ith flat then it will be marked as 1
Next line contains N spaced integers Pj, each represents the maximum allowed persons in the jth flat.
Last line contains N spaced integers Rj, each represents the rent of the jth flat.

Output Format:

Print the number of options i.e. number of different ways in which K friends can have their space and stay connected too.

Constraints:
1<=K<=100
1<=N<=500
1<=T<=10 ^ 6
1<=Rj<=5000
1<=Pj<=100

Sample Input and Output

SNo.InputOutput
1
5 4 10000
0 0 1 1
1 0 0 0
0 1 0 0
0 0 0 1
5 4 3 2
2000 1500 1500 2500

1
2
13 4 10000
0 0 1 1
1 0 0 0
0 1 0 0
0 0 0 1
5 4 3 2
2000 1500 1500 2500

0

A string is said to be palindrome, if it reads the same from both the ends. Given a string S, you are allowed to perform cyclic shifts. More formally, you can pick any one character from any end (head or tail) and you can append that character at the other end. For example, if the string is "abc", then if we do a shift using the character at head position then the string becomes "bca". Similarly, if we do the shift using the character at the tail then the input string becomes "cab". Your task is to find out the minimum number of shifts needed to make the given string, a palindrome. In case, we can't convert the string to palindrome then print -1.

Input Format:

First line starts with T i.e. number of test cases, and then T lines will follow each containing a string "S".

Output Format:

Print the minimum number of cyclic shifts for each string if it can be made a palindrome, else -1.

Constraints:
1<=T<=100
1<=|S|<=300, S will contains only lower case alphabets ('a'-'z').

Sample Input and Output

SNo.InputOutput
1
4
abbb
aaabb
aabb
abc

-1
1
1
-1

Explanation:

For Test Case 2 (aaabb):
Shift the character at the tail to the head and the result will be "baaab", which is a palindrome. This is an operation which requires minimum number of shifts to make the given string a palindrome.

For Test Case 3 (aabb):
One way to convert the given string to palindrome is, shift the character at the head to the tail, and the result will be "abba", which is a palindrome. Another way is to shift the character at the tail to the head, and the result will be "baab", which is also a palindrome. Both require only one shift.

Important Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc