【Codeforces 369C】 Valera and Elections

【链接】 我是链接,点我呀:)
【题意】


给你一棵树
让你选择若干个修理点.
这些修理点被选中之后,节点i到1号节点之间的所有"坏路"都会被修好
问最少需要选择多少个点才能将所有的坏路修好

【题解】


以1为根节点建立一棵树
对于每一棵子树,如果里面有"坏路",必然要选择一个修理点,怎么选择呢?
肯定是"最底下"的那条坏路的下面那个点.
因此记录下每个子树下面的修理点的个数(我们是贪心选择的,能统计出来)
然后如果i和直系儿子j之间有一条坏路,且j所在的子树下面没有修理点,那么这条边肯定只能由一个放在j的修理点修理.
所以在j放一个修理点。
因为是递归的,所以是从下往上,因此满足"最底下"这个条件.
当然i可能有其他的子树,其他的子树的话也按照这样的规则处理就好。
一定要统计这个修理点个数,不然你不知道这条"坏边"是不是所在子树最底下的边(中间的边会被下面的修理点覆盖到不用重新加修理点)

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)1e5;
    static class Task{
        
        ArrayList<Integer> g[] = new ArrayList[N+10];
        ArrayList<Integer> t[] = new ArrayList[N+10];
        ArrayList<Integer> ans = new ArrayList();
        int n;
        
        int dfs(int x,int pre) {
            int len = g[x].size();
            int cnt = 0;
            for (int i = 0;i < len;i++) {
                int y = g[x].get(i),tt = t[x].get(i);
                if (y==pre) continue;
                int kk = dfs(y,x);
                cnt+=kk;
                if (kk==0 && tt==2) {
                    cnt++;
                    ans.add(y);
                }
            }
            return cnt;
        }
        
        public void solve(InputReader in,PrintWriter out) {
            for (int i = 1;i <= N;i++) {
                g[i] = new ArrayList<Integer>();
                t[i] = new ArrayList<Integer>();
            }
            n = in.nextInt();
            for (int i = 1;i <= n-1;i++) {
                int x,y,tt;
                x = in.nextInt();y = in.nextInt();tt = in.nextInt();
                g[x].add(y);t[x].add(tt);
                g[y].add(x);t[y].add(tt);
            }
            dfs(1,0);
            out.println(ans.size());
            for (int i = 0;i < (int)ans.size();i++) {
                out.print(ans.get(i)+" ");
            }
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/10514056.html