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 ...
随机推荐
- grunt yoman bower的理解
grunt : 前端构建工具 1 什么事前端构建工具 目前前端已经开始了工程化 比如 一个项目 里面用到了几十个js文件 几十个css 很多图片资源 我们如果引入 还是按照以前的方式 out 因此这个 ...
- Log4Net 用法记录
https://www.cnblogs.com/lzrabbit/archive/2012/03/23/2413180.html https://blog.csdn.net/guyswj/articl ...
- linux系统下的/proc目录介绍
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- echo---打印变量或输出字符串
cho命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下e ...
- logrotate---日志分割
logrotate命令用于对系统日志进行轮转.压缩和删除,也可以将日志发送到指定邮箱.使用logrotate指令,可让你轻松管理系统所产生的记录文件.每个记录文件都可被设置成每日,每周或每月处理,也能 ...
- 洛谷 P2867 [USACO06NOV]大广场Big Square
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...
- Linux 获取上个月的第一秒和上个月的最后一秒
因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...
- GridView-属性大全
这是个网格控件 他的实现也是通过adapter来实现的,感觉跟listview在使用上并没有多大的区别 常见属性如下 1.android:numColumns=”auto_fit” //GridVie ...
- 21. Node.Js Buffer类(缓冲区)-(一)
转自:https://blog.csdn.net/u011127019/article/details/52512242
- shell-查看手机分辨率
使用如下命令,可以查看手机分辨率 adb shell dumpsys window displays 运行结果如下 Display: mDisplayId= init=1080x1920 480dpi ...