http://codeforces.com/gym/101473/attachments
Problem C
Boss
File: boss.[c|cpp|java]
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 networks in business. As several
\.com” companies, Iks started in a small garage, but today employs hundreds of thousands.
Iks has also a non-standard management system. For example, there are no committees or boards,
which means less time spent on internal meetings. However, as usual in other companies, there is
a chain (or rather several chains) of command, as a person may manage other employees, and may
be managed by other employees. The figure below shows the chain of command for some employees,
along with their ages.
Alice, 21
Clara, 26
David, 33 Elaine, 33
Fred, 22
George, 18
Bia, 42
Alice, 21
Clara, 26
George, 18 Elaine, 33
Fred, 22
David, 33
Bia, 42
(a) (b)
A person P1 may manage another person P2 directly (when P1 is the immediate superior of P1)
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 indirectly. A person does not
manage himself/herself, either directly or indirectly.
One folklore that developed in Wall Street is that Iks is so successfull 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 study its own management system,
and to understand whether age plays a role in its success. You have been hired to work on that tool.
Given the description of the chain of command at Iks and the ages of its employees, 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 George change their respective
positions in the chain of command. A query instruction names one employee 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.
给你一个员工关系图,找一个人的最年轻的领导。过程中可以任意调换两个人的编号和年龄。
这题操作较少,所以每次交换之后直接再次dfs一遍找答案就可以。
交换两个人,只要交换他们对应编号的映射关系就可以,因为原先的管理关系是不变的。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=505,maxk=60005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int head[maxn],val[maxn],du[maxn],a[maxn],b[maxn],p[maxn],u[maxn];
char s[5];
bool visit[maxn];
int num;
struct Edge {
int from,to,pre;
};
Edge edge[maxk*2];
void addedge(int from,int to) {
edge[num]=(Edge){from,to,head[from]};
head[from]=num++;
}
void update(int n) {
mem0(visit);
queue<int> q;
int i;
memcpy(du,b,sizeof(du));
for (i=1;i<=n;i++) {
if (du[i]==0) q.push(i),visit[i]=1;
}
meminf(val);
while (!q.empty()) {
int now=q.front();
q.pop();
for (i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
val[to]=min(val[to],val[now]);
val[to]=min(val[to],a[now]);
du[to]--;
if (du[to]==0)
if (!visit[to]) visit[to]=1,q.push(to);
}
}
}
int main() {
num=0;
memset(head,-1,sizeof(head));
int n,i,j,m,q,x,y;
scanf("%d%d%d",&n,&m,&q);
for (i=1;i<=n;i++) {
scanf("%d",&a[i]);
p[i]=u[i]=i;
}
for (i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
addedge(x,y);
b[y]++;
}
update(n);
for (i=1;i<=q;i++) {
scanf("%s",s);
if (s[0]=='P') {
scanf("%d",&x);
if (val[p[x]]==inf) printf("*\n"); else printf("%d\n",val[p[x]]);
} else {
scanf("%d%d",&x,&y);
int z=a[p[x]];a[p[x]]=a[p[y]];a[p[y]]=z;
z=p[x];p[x]=p[y];p[y]=z;
update(n);
}
}
return 0;
}