HDU 3974 Assign the task 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=3974
题目大意:
一个公司有N个员工,对于每个员工,如果他们有下属,那么他们下属的下属也是他的下属。
公司会给员工安排任务,分配给一个员工后,他也会把这个任务分配给下属。被分配到任务的人立刻停止 当前在做的工作,接受新的任务。
对于给定的M个操作
C x 输出编号为x的任务
T x y 分配任务y给x
思路:
并查集的实现,分配我们只记录在上司结点里,只不过查询的时候要把它的所有上司全部找一遍。
#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN=50001;
int fa[MAXN];
int val[MAXN];
int time[MAXN];
int main()
{
int T;
scanf("%d",&T);
for(int ri=1;ri<=T;ri++)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
fa[i]=i;
val[i]=-1;
time[i]=-1;
} for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
fa[a]=b;
} printf("Case #%d:\n",ri);
int m,cnt=0;
scanf("%d",&m);
char cmd;
while(m--)
{
cin>>cmd;
if(cmd=='C')
{
int x;
scanf("%d",&x);
int cur=x,ans=val[cur],lastt=-1;
while(cur != fa[cur])
{
if(time[cur] > lastt)//后加入的
{
lastt=time[cur];
ans=val[cur];
}
cur=fa[cur];
} if(time[cur] > lastt)//后加入的
{
lastt=time[cur];
ans=val[cur];
}
printf("%d\n",ans);
}
else
{
int x,y;
scanf("%d%d",&x,&y);
val[x]=y;
time[x]=cnt++;
}
} }
return 0;
}
HDU 3974 Assign the task 并查集的更多相关文章
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 3974 Assign the task 暴力/线段树
题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 3974 Assign the task
Assign the task Problem Description There is a company that has N employees(numbered from 1 to N),ev ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)
描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...
- hdu 3974 Assign the task(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...
- hdu 3974 Assign the task (线段树+树的遍历)
Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
随机推荐
- RPC简易学习
0.RPC简介 RPC, 英文全称:Remote Process Call. 中文全称:远程过程调用. 客户端通过网络请求调用远程服务端对外暴露服务.常用的两种RPC协议:TCP.HTTP. ...
- ST和LCA和无根树连接
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm& ...
- 03006_DOS操作数据乱码解决
1.我们在dos命令行操作中文时,会报错 insert into sort(sid,sname) values(2,"电视机"); ERROR 1366 (HY000): Inco ...
- 【Educational Codeforces Round 36 D】 Almost Acyclic Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...
- HDU——T 4738 Caocao's Bridges
http://acm.hdu.edu.cn/showproblem.php?pid=4738 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- worktools-git 工具的使用总结(3)
1.标签的使用,增加标签 git tag 1.0 branch_name zhangshuli@zhangshuli-MS-:~/myGit$ git br -av parent e2e09c4 so ...
- dump var_dump print print_r的区别
dump var_dump print print_r的区别 一.总结 用dump()来打印就对了 1.echo和print:不能打印复合型和资源型数据: 2.var_dump()和print_r() ...
- 2.3 Streams API 官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.3 Streams API 2.3 Streams API 在0..0增加了一个 ...
- SVN和Git代码管理小结
SVN和Git代码管理小结 之前,先用的是SVN,后来用了Git,最近又在用SVN. 关于代码管理,写几句. 由于自己参与的项目,人通常不超过10个人,版本不是很多,协作比较正常,感觉SVN ...
- ios 获取手机信息(UIDevice、NSBundle、NSLocale)
iOS的SDK中提供了UIDevice.NSBundle,NSLocale. UIDevice UIDevice提供了多种属性.类函数及状态通知,帮助我们全方位了解设备状况. 从检測电池 ...