Codeforces Round 926 (Div. 2) ---- E. Sasha and the Happy Tree Cutting ----题解

E. Sasha and the Happy Tree Cutting:

题目大意:

思路解析:

现在有一颗树,然后给出了k对路径,然后要求路径上至少有一个结点是被染色了的,如果这k对路径没有共用边,那我们至少需要染色k条边。如果有共用边,那么选择将他染色,那么就可以使多个路径满足条件。现在求最小是染色树。已知最多需要20条边,那我们可以找出哪些边可以进行染色(并且它染色后能解决哪些路径),然后进行状压dp。

代码实现:

复制代码
import java.util.*;


import java.io.*;
import java.math.*;

public class Main {
    static int[] pre;
    static Vector<int[]>[] g;
    public static void main(String[] args) throws IOException {
        int t = f.nextInt();
        while (t > 0) {
            solve();
            t--;
        }
        w.flush();
        w.close();
    }

    public static void solve() throws IOException{
        int n = f.nextInt();
        g = new Vector[n+1];
        pre = new int[n+1];
        for (int i = 0; i < n + 1; i++) {
            g[i] = new Vector<>();
        }
        for (int i = 0; i < n - 1; i++) {
            int x = f.nextInt();
            int y = f.nextInt();
            g[x].add(new int[] {y, i});
            g[y].add(new int[] {x, i});
        }
        int[] st = new int[n];
        dfs1(1, 0);
        int k = f.nextInt();
        for (int i = 0; i < k; i++) {
            int x = f.nextInt(); int y = f.nextInt();
            if (pre[x] != 0) dfs2(x);

            while ((x ^ y) != 0){
                for (int j = 0; j < g[y].size(); j++) {
                    int[] cur = g[y].get(j);
                    if (cur[0] == pre[y]){
                        y = pre[y];
                        st[cur[1]] |= (1 << i);
                        break;
                    }
                }
            }
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (st[i] > 0)
                set.add(st[i]);
        }
        int[] dp = new int[1 << k];
        Arrays.fill(dp, 1000);
        dp[0] = 0;
        for (int i = 0; i < (1 << k); i++) {
            for (Integer j: set){
                dp[i | j] = Math.min(dp[i|j], dp[i] + 1);
            }
        }
        w.println(dp[(1 << k) - 1]);
    }

    public static void dfs1(int x, int fa){
        for (int i = 0; i < g[x].size(); i++) {
            int[] cur = g[x].get(i);
            int y = cur[0];
            if (y == fa) continue;
            pre[y] = x;
            dfs1(y, x);
        }

    }

    public static void dfs2(int x){
        int y = pre[x];
        pre[x] = 0;
        while (y != 0){
            int t = pre[y];
            pre[y] = x;
            x = y;
            y = t;
        }
    }



    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() throws IOException{
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public Double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}
相关推荐
BB_CC_DD4 分钟前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘1 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV2 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8242 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify2 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
shix .2 小时前
2025年PTA天梯赛正式赛 | 算法竞赛,题目详解
数据结构·算法
风铃儿~2 小时前
Java面试高频问题(26-28)
java·算法·面试
wuqingshun3141592 小时前
蓝桥杯 4. 卡片换位
算法·职场和发展·蓝桥杯
江沉晚呤时2 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
Eric.Lee20213 小时前
数据集-目标检测系列- F35 战斗机 检测数据集 F35 plane >> DataBall
人工智能·算法·yolo·目标检测·计算机视觉