[URI ONLINE JUDGE] – 1469 – Boss

Everyone knows Iks, the last trend in social network, which made so much success that competitors like Facebook and Google+ are strugling to keep their own social networ ks in business. As several “.com” companies, Iks started in a small garage, but today employs hundr eds of thousands. Iks has also a non-standard management system. For example, there are no c ommittees or boards, which means less time spent on internal meetings. However, as usual i n other companies, there is a chain (or rather several chains) of command, as a person may manage other emp loyees, and may be managed by other employees. The figure below shows the chain of comman d for some employees, along with their ages.

A person P1 may manage another person P2 directly (when P1 is the immediate superior of P2 ) or indirectly (when P1 manages direclty a person P3 who manages P2 directly or indirectly). For example, in the figure above, Alice manages David directly and Claire in directly. A person does not manage himself/herself, either directly or indirectly. One folklore that developed in Wall Street is that Iks is so successf ull because in its chain of command a manager is always younger than the managed employee. As we can see in figure above, that is not true. But this folklore prompted Iks to develop a tool to st udy its own management system, and to understand whether age plays a role in its success. You have be en hired to work on that tool. Given the description of the chain of command at Iks and the ages of its empl oyees, write a program that answers a series of instructions. Instructions are of two types : management change and query. An instruction of management change swaps the positions of two employees A and B . As an example, figure (b) above shows the resulting chain of command when David and Ge orge change their respective positions in the chain of command. A query instruction names one employ ee A and asks the age of the youngest manager of A ; that is, the youngest person in the chain of command at Iks that manages A , either directly or indirectly. For example, in figure (a) above the youngest manager of Clara is 18 years old; in figure (b), the youngest manager of Clara is 21 years old.

Input

The input contains several test cases and ends with EOF. Each test case is described using several lines. The first line of a test case contains three integers N (1 ≤ N ≤ 500), M(0 ≤ M ≤ 60 × 103) and I(1 ≤ I ≤ 500) , representing respectively the number of employees, the number of direct manage relations and the number of instructions. E mployees are identified by numbers from 1 to N . The second line contains Nintegers Ki(1 ≤ Ki ≤ 100, para 1 ≤ iN) , where Ki indicates the age of the employee number i .

Each of the following M lines contains two integers X and Y(1 ≤ X, YN, X != Y) , indicating that X managesY directly. Then it follows I lines, each describing an instruction. An instruction of type managem ent change is described by a line containing the identifier T followed by two integers A and B (1 ≤ A,BN) , indicating the two employers that must swap places in the chain of command. An instruction of type query is described by a line containing the identifier P followed by one integer E(1 ≤ EN) , representing the number of an employer. The last instruction is of type query.

Output

For each instruction of type query your program must print a single line containing a single integer, the age of the employee who is the youngest person that manages the employ er named in the query (directly or indirectly), if that person exists. If the employe e does not have a manager, print an ‘*’ (asterisc).

Sample Input Sample Output
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
18
21
18
18
*
26

my solution:

<br />#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <stack>

using namespace std;
typedef vector<vector<int> > graph;

int find (vector<int> &def, int u){
for (int i=0; i<def.size(); i++){
if (def[i] == u) return i;
}
}

void change (vector<int> &ages, vector<int> &def, int u, int v){
swap (ages[def[u]], ages[def[v]]);
swap (def[u], def[v]);
}

void pvector (vector<int> &def){
for (int i=0; i<def.size(); i++)
cout << def[i]+1 << ' ';
cout << endl;
}

int dfs (graph &g, vector<int> &age, vector<int> &def, int start){

int n = g.size();
stack<int> s;
vector<int> visited (n, 0);
int atual, next, ans = 99999999;
s.push (start);
visited[start] = 1;
while (!s.empty()){
atual = s.top();
s.pop();

for (int i=0; i<g[atual].size(); i++){
next = g[atual][i];

if (visited[next]) continue;
ans = min (ans, age[next]);
s.push (next);
visited[next] = 1;
}
}
return ans;
}

void print (graph &g){

for (int i=0; i<g.size(); i++){
cout << i+1 << ": ";
for (int j=0; j<g[i].size(); j++){
cout << g[i][j]+1 << ' ';
}
cout << endl;
}
}

int main(){
int n, m, I;
graph g;
vector<int> ages, def;
while (true){
cin >> n >> m >> I;

if (!cin) break;

g.clear();
ages.clear();
def.clear();

ages.resize (n, 0);
def.resize (n);
g.resize (n);

for (int i=0; i<n; i++){
cin >> ages[i];
def[i] = i;
}
for (int i=0; i<m; i++){
int u, v;
cin >> u >> v;
g[v-1].push_back (u-1);
}
for (int i=0; i<I; i++){
char q;
int u, v;
cin >> q;
if (q == 'T'){
cin >> u >> v;
change (ages, def, u-1, v-1);
}
else {
cin >> u;
int ans = dfs (g, ages, def, def[u-1]);
if (ans == 99999999)
cout << "*\n";
else
cout << ans << endl;
}
}
}

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s