您的位置:首页 > 其它

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;
}
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: