HDU 3974 Assign the task (并查集 || 线段树)
2017-12-19 14:04
447 查看
题目链接
虽然挂在线段树的题集里,但是能一眼看出也是并查集的题。本来准备就用线段树做,可是,想了半天不知道怎么建树。。。就先用并查集过吧。等忙完期末项目,回来再看看怎么用线段树做。
并查集代码:
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;
}
}
}
}
}
虽然挂在线段树的题集里,但是能一眼看出也是并查集的题。本来准备就用线段树做,可是,想了半天不知道怎么建树。。。就先用并查集过吧。等忙完期末项目,回来再看看怎么用线段树做。
并查集代码:
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;
}
}
}
}
}
相关文章推荐
- HDU 3974 Assign the task (DFS序 + 线段树)
- HDU 3974 Assign the task【DFS序+线段树区间染色】
- HDU 3974 Assign the task(dfs序 + 线段树区间赋值,单点查询)
- HDU - 3974 Assign the task —— 并查集
- HDU 3974 Assign the task (线段树)
- HDU 3974 Assign the task(dfs时间戳+线段树成段更新)
- HDU 3974 Assign the task (DFS+线段树)
- HDU - 3974 Assign the task(线段树或暴力)
- HDU 3974 Assign the task 线段树(树映射到区间)
- J - Assign the task - hdu 3974(DFS建树+简单线段树)
- HDU - 3974 Assign the task(线段树区间更新+DFS序)
- HDU 3974 Assign the task [并查集扩展]
- HDU 3974 Assign the task(简单线段树)
- HDU - 3974 Assign the task (DFS序 + 线段树区间染色)
- 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时间戳+线段树成段更新)