Codeforces Gym 101473C Boss 拓扑排序

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。GymCodeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值