Assign the task (并查集)
来源:互联网 发布:淘宝网店怎么发布商品 编辑:程序博客网 时间:2024/06/05 18:00
题目是这样的:公司里有N个职工(公司老板也是其中一名)(1--N编号),职工分等级,公司老板是终极大boss,管理所有员工,然后分层,每层都有若干boss,手底下有若干手下,直到最底层的无产阶级;除了大boss以外,每个boss都有上司,即boss的boss, 而boss的手下也属于boss的上司的手下;现在开始分配任务,当一个人分配到任务后,他所有的手下都停下手头工作,来处理这个工作;问x号员工手头的工作是什么;
并查集就好了,不过需要开一个存时间的数组,而且不能压缩路径;
上代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#define MAX 50000+5using namespace std;int uset[MAX];int t[MAX]; //存时间:t[2]=4表示2号手头的工作是4时间开始的(后面的工作覆盖前面的工作);int ans[MAX]; //存工作;ans[3]=1表示3号手头工作是1号工作;int findd(int x){ int r=x; int ed=x; //ed存x的boss中工作时间最晚的; int time=t[x]; while(uset[r]!=r){ if(t[uset[r]]>=time){ //r的boss工作时间大于等于r的工作时间,r开始干boss的工作; time=t[uset[r]]; ed=uset[r]; } r=uset[r]; } return ed;}void query(int x){ //打印x的工作; printf("%d\n",ans[findd(x)]);}void ass(int x, int y){ //x的工作是y; ans[x]=y;}int main(){ int T; scanf("%d",&T); int cnt=0; while(T--){ cnt++; memset(ans, -1, sizeof(ans)); memset(t, 0, sizeof(t)); int n; scanf("%d",&n); for(int i=1; i<=n; i++) uset[i]=i; for(int i=1; i<n; i++){ int son, father; scanf("%d %d",&son,&father); uset[son]=father; } printf("Case #%d:\n",cnt); int m; scanf("%d",&m); for(int i=1; i<=m; i++){ char ch[5]; //由于输入单个字符,后边还有空格,所以直接输入字符串,不用考虑字符输入的处理; int x, y; scanf("%s",ch); if(ch[0]=='C'){ scanf("%d",&x); query(x); } else{ scanf("%d%d",&x,&y); t[x]=i; ass(x,y); } } } return 0;}
阅读全文
0 0
- Assign the task (并查集)
- Assign the task (并查集)
- HDU 3974 Assign the task (并查集改编)
- HDU 3974 Assign the task 并查集
- HDU 3974 Assign the task(并查集)
- HDU 3974 Assign the task(树 并查集)
- HDU 3974 Assign the task [并查集扩展]
- HDU 3974 Assign the task(并查集)
- 并查集--HDU 3974Assign the task--带权
- HDU 3974 Assign the task 并查集解法
- HDU 3974 Assign the task (并查集&线段树)
- HDOJ Assign the task
- hdu3974 Assign the task
- hdu3974----Assign the task
- HDU3974-Assign the task
- Assign the task
- Assign the task HDU
- Assign the task
- Toolbar标题居中怎么设置
- Android设备命令打开adb
- AppDomain
- javaWeb项目的classpath说明
- 创建Java线程的3种方式及对比
- Assign the task (并查集)
- hdu3068 manacher模版
- Java NIO 概述
- django提交表单报错"You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SL
- java中equals()和“==”的区别
- [NOIP提高组2004]虫食算
- python学习:if...else
- GUI系统如何确定鼠标事件所属的控件
- hdu2136 ----Largest prime factor