|
| 1 | +#include <stdio.h> |
| 2 | +#include <vector> |
| 3 | +#include <bitset> |
| 4 | +#include <memory.h> |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +struct node { |
| 10 | + int id, age; |
| 11 | + bitset<501> parents; |
| 12 | + |
| 13 | + node(int id, int age) { |
| 14 | + this->id = id; |
| 15 | + this->age = age; |
| 16 | + parents.reset(); |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +int n, m, q; |
| 21 | +bool vis[501]; |
| 22 | +char ch; |
| 23 | +vector<node> nodes; |
| 24 | + |
| 25 | +int get_min_age(int x) { |
| 26 | + vis[x] = true; |
| 27 | + int res = 101; |
| 28 | + for(int i = 0; i < n; ++i) |
| 29 | + if(nodes[x].parents[i] && !vis[i]) |
| 30 | + res = min(res, min(nodes[i].age, get_min_age(i))); |
| 31 | + return res; |
| 32 | +} |
| 33 | + |
| 34 | +int find(int x) { |
| 35 | + for(int i = 0; i < n; ++i) |
| 36 | + if(nodes[i].id == x) |
| 37 | + return i; |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +int main() { |
| 42 | + while(scanf("%d %d %d", &n, &m, &q) != EOF) { |
| 43 | + nodes.clear(); |
| 44 | + |
| 45 | + for(int i = 0, age; i < n; ++i) { |
| 46 | + scanf("%d", &age); |
| 47 | + nodes.push_back(node(i, age)); |
| 48 | + } |
| 49 | + |
| 50 | + for(int i = 0, a, b; i < m; ++i) { |
| 51 | + scanf("%d %d", &a, &b); |
| 52 | + --a, --b; |
| 53 | + nodes[b].parents[a] = 1; |
| 54 | + } |
| 55 | + |
| 56 | + for(int i = 0, a, b; i < q; ++i) { |
| 57 | + scanf(" %c", &ch); |
| 58 | + |
| 59 | + if(ch == 'T') { |
| 60 | + scanf("%d %d", &a, &b); |
| 61 | + --a, --b; |
| 62 | + a = find(a); |
| 63 | + b = find(b); |
| 64 | + swap(nodes[a].id, nodes[b].id); |
| 65 | + swap(nodes[a].age, nodes[b].age); |
| 66 | + } else { |
| 67 | + scanf("%d", &a); |
| 68 | + --a; |
| 69 | + a = find(a); |
| 70 | + if(nodes[a].parents.none()) puts("*"); |
| 71 | + else { |
| 72 | + memset(vis, false, sizeof vis); |
| 73 | + printf("%d\n", get_min_age(a)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
0 commit comments