HDU 3974 Assign the task (并查集&线段树)
来源:互联网 发布:上海市软件著作权资助 编辑:程序博客网 时间:2024/05/16 12:50
题目链接
虽然挂在线段树的题集里,但是能一眼看出也是并查集的题。本来准备就用线段树做,可是,想了半天不知道怎么建树。。。就先用并查集过吧。等忙完期末项目,回来再看看怎么用线段树做。
并查集代码:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.Scanner;public class Main {static Scanner sc = new Scanner(System.in);static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));static int T,n,m,newTime,newTask;static int [] dad;static int [] mis;static int [] tim;static void find(int x){while(dad[x] != x){if(newTime < tim[x]){//newTime小则应跟随大的时间更新newTime = tim[x];newTask = mis[x];}x = dad[x];}if(newTime < tim[x]){//与最终boss比较newTime = tim[x];newTask = mis[x];}}public static void main(String[] args) throws IOException {T = sc.nextInt();for(int t = 1; t <= T; t++){System.out.println("Case #"+t+":");n = sc.nextInt();dad = new int[n+1];for(int i = 1; i <= n; i++)dad[i] = i;mis = new int[n+1];Arrays.fill(mis, -1);tim = new int[n+1];Arrays.fill(tim, 0);int u,v;for(int i = 0; i < n - 1; i++){u = sc.nextInt(); v = sc.nextInt();dad[u] = v;}m = sc.nextInt();String op;int emp,task,k = 0;for(int i = 0; i < m; i++){op = sc.next();if(op.equals("C")){emp = sc.nextInt();newTime = 0;newTask = mis[emp];find(emp);System.out.println(newTask);}else{emp = sc.nextInt(); task = sc.nextInt();mis[emp] = task;tim[emp] = ++k;}}}}}
阅读全文
0 0
- 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 3974 Assign the task [并查集扩展]
- 并查集--HDU 3974Assign the task--带权
- HDU 3974 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 (线段树+dfs序)
- HDU 3974 Assign the task(DFS建树+线段树)
- Assign the task (并查集)
- HDU - 3974 Assign the task(线段树 区间修改)
- 11_等待/通知(wait/notify)机制介绍
- 使用MockMVC测试控制器【转】
- SD卡文件权限
- 异步上传图片-不压缩图片
- 进程调度API之init_waitqueue_head
- HDU 3974 Assign the task (并查集&线段树)
- 如何登陆服务器
- TCP三次握手wireshark抓包分析
- 数据结构-抽象数据类型
- php技能树
- Linux系统下强制踢掉登录用户
- Wget用法、参数解释的比较好的一个文章
- kong插件之Rate Limiting
- 【sql server 2008】从零开始的数据库复习之路(一)