您的位置:首页 > 其它

Boss

2016-07-08 17:36 225 查看

Boss









题意:n个人,m个从属关系,i次操作,T开头代表把两人职位对调,P要求搜索对应人的上司中职位最年轻的

解法:应用指针对调,搜索时直接dfs

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int age[600], last[600], point[600], n, m, q, x, y, ans;
int a[70000], nnext[70000], tot, d[600];
bool flag[600];

void insert(int x, int y) {
tot++;
a[tot] = y;
nnext[tot] = last[x];
last[x] = tot;
}

int main() {
while (scanf("%d %d %d", &n, &m, &q) != EOF) {
memset(last, 0, sizeof(last));
for (int i = 1; i <= n; i++) {
cin >> age[i];
point[i] = i;
}
tot = 0;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
insert(y, x);
}
char c;
for (int qq = 1; qq <= q; qq++) {
cin >> c;
if (c == 'P') {
memset(flag, 1, sizeof(flag));
cin >> x;
d[0] = 1;
d[1] = point[x];
ans = -1;
int i = 0, j = 1;
while (i < j) {
i++;
int k = last[d[i]];
while (k != 0) {
if (flag[a[k]]) {
flag[a[k]] = false;
d[++j] = a[k];
}
k = nnext[k];
}
}
for (int i = 1; i <= n; i++) if (!flag[point[i]]) {
if (age[i] < ans || ans == -1)
ans = age[i];
}
if (ans == -1) cout << "*" << endl;
else cout << ans << endl;
} else {
cin >> x >> y;
int temp = point[x];
point[x] = point[y];
point[y] = temp;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: