感觉题目数据有问题
查看原帖
感觉题目数据有问题
657442
wusihao1931楼主2023/11/28 22:05

// 题目说保证第e条边存在 // 但是我加了第55行代码能通过 // 但是去掉了报运行时异常

package algorithm;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;


public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static StreamTokenizer sk = new StreamTokenizer(br);
    static int []p, w, ans;
    static List<int[]> []ops, g;
    static boolean []isDeleted;
    static int n, m, idx;
    static int find(int x) {
        if (x != p[x]) {
            p[x] = find(p[x]);
        }

        return p[x];
    }
    static void merge(int a, int b) {
        int pa = find(a), pb = find(b);
        if (pa != pb) {
            p[pa] = pb;
            w[pb] += w[pa];
        }
    }
    public static void main(String[] args) throws Exception {
        n = nextInt();
        m = nextInt();
        p = new int[n + 1];
        g = new List[n + 1];
        w = new int[n + 1];
        isDeleted = new boolean[n + 1];
        ops = new ArrayList[m + 1];
        ans = new int[m + 1];
        for (int i = 0; i <= n; i ++ ) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i <= m; i ++ ) {
            ops[i] = new ArrayList<>();
        }
        for (int i = 1; i <= n; i ++ ) {
            w[i] = nextInt();
        }

        for (int i = 1; i < n; i ++ ) {
            int a = nextInt(), b = nextInt();
            g[i].add(new int[] {a, b});
        }
        g[n].add(new int[2]);

        for (int i = 1; i <= m; i ++ ) {
            int op = nextInt();
            int []t = new int[10];
            t[0] = op;
            if (op == 1) {
                int u = nextInt();
                t[1] = u;
                ops[i].add(t);
                isDeleted[u] = true;
            } else if (op == 2) {
                int u = nextInt(), v = nextInt();
                t[1] = u;
                t[2] = w[u];
                t[3] = v;
                ops[i].add(t);
                w[u] = v;
            } else if (op == 3) {
                int u = nextInt();
                t[1] = u;
                t[4] = ++ idx;
                ops[i].add(t);
            }
        }

        for (int i = 1; i <= n; i ++ ) {
            p[i] = i;
        }

        for (int i = 1; i < n; i ++ ) {
            if (!isDeleted[i]) {
                merge(g[i].get(0)[0], g[i].get(0)[1]);
            }
        }

        for (int i = m; i >= 1; i -- ) {
            int op = ops[i].get(0)[0];
            if (op == 1) {
                int u = ops[i].get(0)[1];
                int a = g[u].get(0)[0], b = g[u].get(0)[1];
                merge(a, b);
            } else if (op == 2) {
                int u = ops[i].get(0)[1];
                int before = ops[i].get(0)[2];
                int after = ops[i].get(0)[3];
                int pu = find(u);
                w[pu] += (before - after);
            } else if (op == 3) {
                int u = ops[i].get(0)[1];
                int id = ops[i].get(0)[4];
                ans[id] = w[find(u)];
            }
        }

        for (int i = 1; i <= idx; i ++ ) {
            pw.println(ans[i]);
        }
        pw.flush();
    }




    public static int nextInt() throws Exception {
        sk.nextToken();
        return BigDecimal.valueOf(sk.nval).intValue();
    }

    public static long nextLong() throws Exception {
        sk.nextToken();
        return BigDecimal.valueOf(sk.nval).longValue();
    }
}

2023/11/28 22:05
加载中...