Under construction

Still being built. Things may change

Youngest Boss

Time limit1sMemory limit512 MB

Summary
Given a directed acyclic chain of command, process swaps of two employees and report the age of the youngest superior of a queried employee, or * if none exists.
Level

Medium5 of 10

Topics
Graph, DFS, Implementation
Solved
No attempts yet

Problem

A company has no committees and no boards. The only structure is the set of management relations between employees, and one employee may manage other employees while being managed by other employees.

An employee P1 manages an employee P2 in one of two ways. P1 manages P2 directly when P1 is the immediate superior of P2. P1 manages P2 indirectly when P1 directly manages an employee P3 who manages P2 directly or indirectly. No employee manages himself or herself, directly or indirectly.

The figure below shows the chain of command of seven employees with their ages. Part (a) is the starting state, and part (b) is the state after two employees swap their positions in the chain of command.

The company wants to know what role age plays in its organization, so it is building a tool that analyzes the chain of command. The tool processes the instructions in the order they are given. There are two kinds of instruction, a management change and a query.

A management change instruction swaps the positions of employee A and employee B. The positions and the management relations between positions stay as they are, and the two employees exchange positions, each keeping his or her own age. When A and B are the same employee, nothing changes.

A query instruction names an employee E and asks for the age of the youngest employee who manages E directly or indirectly at that moment.

Given the chain of command, the age of every employee and the list of instructions, write a program that answers every query.

Input

The first line contains the number of employees NN, the number of direct management relations MM and the number of instructions II, separated by spaces. Employees are identified by the numbers 11 to NN.

The second line contains NN integers K1,K2,…,KNK_1, K_2, \dots, K_N, where KiK_i is the age of employee ii.

Each of the next MM lines contains two integers XX and YY, meaning that XX manages YY directly.

Each of the next II lines contains one instruction. A management change instruction consists of the character T and two integers AA, BB, and employees AA and BB swap positions. A query instruction consists of the character P and one integer EE, and asks for the age of the youngest employee who manages employee EE. The last instruction is always a query.

Constraints

  • 1≤N≤5001 \le N \le 500
  • 0≤M≤600000 \le M \le 60000
  • 1≤I≤5001 \le I \le 500
  • 1≤Ki≤1001 \le K_i \le 100 (1≤i≤N1 \le i \le N)
  • 1≤X,Y≤N1 \le X, Y \le N, X≠YX \ne Y
  • 1≤A,B≤N1 \le A, B \le N
  • 1≤E≤N1 \le E \le N
  • The management relations given in the input contain no cycle.

Output

For each query instruction, print one line. If at least one employee manages the named employee directly or indirectly, print the age of the youngest such employee. Otherwise print a single asterisk (*).

Examples7

  1. Example 1

    Input
    7 8 9
    21 33 33 18 42 22 26
    1 2
    1 3
    2 5
    3 5
    3 6
    4 6
    4 7
    6 7
    P 7
    T 4 2
    P 7
    P 5
    T 1 4
    P 7
    T 4 7
    P 2
    P 6
    
    Expected output
    18
    21
    18
    18
    *
    26
    
  2. Example 2

    Input
    6 5 6
    10 20 30 40 50 60
    1 5
    1 4
    3 6
    2 5
    4 5
    P 1
    P 5
    P 6
    T 1 6
    P 1
    P 4
    
    Expected output
    *
    10
    30
    30
    60
    
  3. Example 3

    Input
    1 0 1
    50
    P 1
    
    Expected output
    *
    
  4. Example 4

    Input
    4 0 6
    1 100 50 50
    P 1
    T 1 4
    P 4
    P 2
    T 2 2
    P 3
    
    Expected output
    *
    *
    *
    *
    
  5. Example 5

    Input
    6 5 9
    60 50 40 30 20 10
    1 2
    2 3
    3 4
    4 5
    5 6
    P 1
    P 6
    T 1 6
    P 6
    P 1
    T 2 3
    P 4
    P 3
    P 2
    
    Expected output
    *
    20
    *
    10
    10
    10
    10
    
  6. Example 6

    Input
    4 4 7
    5 7 7 9
    1 2
    1 3
    2 4
    3 4
    P 4
    T 3 3
    P 4
    T 1 4
    P 4
    P 1
    P 2
    
    Expected output
    5
    5
    *
    7
    9
    
  7. Example 7

    Input
    6 4 8
    10 20 30 40 50 60
    1 2
    2 3
    4 5
    5 6
    P 3
    P 6
    T 1 4
    P 3
    P 6
    T 3 6
    P 3
    P 6
    
    Expected output
    10
    40
    20
    10
    10
    20