hdu 3974 Task assign(哈希暴力 or dfs时间戳+线段树)

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody’s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
Input
The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.

For each test case:

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).

The next line contains an integer M (M ≤ 50,000).

The following M lines each contain a message which is either

“C x” which means an inquiry for the current task of employee x

or

“T x y”which means the company assign task y to employee x.

(1<=x<=N,0<=y<=10^9)
Output
For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.

Sample Input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3

Sample Output
Case #1:
-1
1
2

卧槽这题我打了两天= =。刚接触acm的时候就把这题a了,那时候用的是哈希暴力:

public class TaskAssign {
    static HashMap works = new HashMap();
    public static void main(String[] args) {
        InputReader scanner = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int t = scanner.nextInt();
        int cases = 1;
        while (t-- > 0) {
            out.println("Case #" + cases + ":");
            cases++;
            int n = scanner.nextInt();
            Employee[] employees = new Employee[n + 1];
            for (int i = 0; i <= n; i++) {
                employees[i] = new Employee();
            }
            for (int i = 1; i < n; i++) {
                int u = scanner.nextInt();
                employees[scanner.nextInt()].sub.add(u);
            }
            int m = scanner.nextInt();
            for (int i = 0; i < m; i++) {
                String operate = scanner.next();
                int man = scanner.nextInt();
                switch (operate) {
                case "C":
                    if (works.containsKey(man)) {
                        out.println(works.get(man));
                    } else {
                        out.println(-1);
                    }
                    break;
                case "T":
                    int task = scanner.nextInt();
                    works.put(man, task);
                    if (!employees[man].sub.isEmpty()) {
                        newTask(man, task, employees);
                    }
                    break;
                }
            }
            works.clear();
        }
        out.close();
    }

    public static void newTask(int man,int task,Employee[] employees) {
        Employee get = employees[man];
        for (int j = 0, len = get.sub.size(); j < len; j++) {
            works.put(get.sub.get(j), task);
            newTask(get.sub.get(j), task, employees);
        }
    }
}

class Employee {
    ArrayList sub = new ArrayList();
}

代码短,速度快,我以为这题的标准解法就这样了….

后来才知道世界上有种东西叫胸都给你拍扁(不对)…..叫把树拍扁,线段树维护。

然后就陷入了无限找bug…..发现原来Arrays.fill搞不定自定义数据结构的初始化……

public class dfsOrder {
    static int[] start = new int[50005];
    static int[] end = new int[50005];// 时间戳
    static Set vis = new HashSet<>();
    static int index = 0;
    static Node[] tree = new Node[50005 * 4 + 1];
    @SuppressWarnings("unchecked")
    static ArrayList G[] = new ArrayList[50005];

    public static void main(String[] args) {
        // Scanner reader = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        InputReader reader = new InputReader();
        int t = reader.nextInt();
        int cases = 1;
        for (int i = 0; i < 50000; i++) {
            G[i] = new ArrayList<>();
        }
        for (int i = 0; i <= 50005 << 2; i++) {
            tree[i] = new Node();
        }
        while (t-- > 0) {
            int n = reader.nextInt();
            for (int i = 0; i <= n; i++) {
                G[i].clear();
            }
            out.println("Case #" + (cases++) + ":");
            for (int i = 1; i < n; i++) {
                int u = reader.nextInt();
                int v = reader.nextInt();
                G[v].add(u);
                vis.add(u);
            }

            // if (G[4].size() > 0) {
            // for (int i = 0; i < G[4].size(); i++) {
            // System.out.print(G[4].get(i) + " ");
            // }
            // return;
            // }
            index = 0;
            for (int i = 1; i <= n; i++) {
                if (!vis.contains(i)) {// 找根!
                    dfs(i);
                    break;
                }
            }
            vis.clear();
            int m = reader.nextInt();
            build(1, 1, n);
            while (m-- > 0) {
                String query = reader.next();
                switch (query) {
                case "C":
                    int x = reader.nextInt();
                    out.println(query(1, start[x]));
                    break;
                case "T":
                    int u = reader.nextInt();
                    int task = reader.nextInt();
                    update(1, start[u], end[u], task);
                    break;
                default:
                    break;
                }
            }
        }
        out.close();
    }

    // dfs序时间戳
    public static void dfs(int x) {
        start[x] = ++index;
        for (int i = 0; i < G[x].size(); i++) {
            dfs(G[x].get(i));
        }
        end[x] = index;
    }

    public static void build(int i, int l, int r) {
        tree[i].l = l;
        tree[i].r = r;
        tree[i].task = -1;
        tree[i].lazy = false;
        if (l == r) {
            return;
        }
        int mid = (l + r) / 2;
        build(i << 1, l, mid);
        build(i << 1 | 1, mid + 1, r);
    }

    public static void update(int i, int l, int r, int task) {
        if (tree[i].l == l && tree[i].r == r) {
            updateSame(i, task);
            return;
        }
        pushDown(i);
        int mid = (tree[i].l + tree[i].r) / 2;
        if (r <= mid) {
            update(i << 1, l, r, task);
            return;
        }
        if (l > mid) {
            update(i << 1 | 1, l, r, task);
            return;
        }
        update(i << 1, l, mid, task);
        update(i << 1 | 1, mid + 1, r, task);
    }

    public static int query(int i, int u) {
        if (tree[i].l == u && tree[i].r == u) {
            return tree[i].task;
        }
        pushDown(i);
        int mid = (tree[i].l + tree[i].r) / 2;
        if (u <= mid) {
            return query(i << 1, u);
        }
        return query(i << 1 | 1, u);
    }

    public static void updateSame(int i, int task) {
        if (i == 0) {
            // return;
        }
        tree[i].task = task;
        tree[i].lazy = true;
    }

    public static void pushDown(int i) {
        if (!tree[i].lazy) {
            return;
        }
        updateSame(i << 1, tree[i].task);
        updateSame(i << 1 | 1, tree[i].task);
        tree[i].lazy = false;
    }


    static class Node {
        int l;
        int r;
        int task;
        boolean lazy;
    }
}

贼长啊好吗…..

你可能感兴趣的:(ACM,线段树)