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

【题意】

给你一棵树
让你选择若干个修理点.
这些修理点被选中之后,节点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());
}
}
}

【Codeforces 369C】 Valera and Elections的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. matlab进入指定目录

    cd C:\Users\hui\Desktop\minepy\1\minepy-1.2.0\minepy-1.2.0\matlab

  2. ffmpeg 有用命令 (转载)

    转自:http://blog.csdn.net/simongyley/article/details/9984167 1.将h264文件解码为yuv文件 ffmpeg -i file.h264 fil ...

  3. 【WIP_S3】链表

    创建: 2017/12/26 完成: 2018/01/14   [TODO]     S4, S5, S14来处理动态数组   CAF8A81B790F [github 地址]传送门  链表的定义   ...

  4. 栗染-github中搭建博客遇到的问题之一

    运行命令:git push -u origin master To https://github.com/xuzhezhaozhao/Practice.git ! [rejected] master ...

  5. js获取标签的三种方式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. C#与C++的区别(二)

    这几天深入学习C#的面向对象的内容,发现C#的很多用法跟C++比起来还是有很多的不同点,头脑中知识的海洋刮起了阵阵海浪,在此继续整理一下二者的不同点,主要还是写的C#能用,而在C++中不能用的一些知识 ...

  7. .net引用System.Data.SQLite操作SQLite

    之所以要做这个笔记,是因为在.NET中使用System.Data.SQLite的时候,遇到了些问题,这些问题是相对于引用其他dll没有遇到过的,所以作个笔记,记录一下. 简单起见,首先建立一个控制台项 ...

  8. [转]iOS WebKit browsers and auto-zooming form controls

    问题描述:https://github.com/jquery/jquery-mobile/issues/2581 本文转自:http://www.456bereastreet.com/archive/ ...

  9. NodeJs学习记录(五)初学阶段关于ejs和路由

    1.因为只是用了一点皮毛,所以使用起来感觉基本和jsp无异, 逻辑代码块使用  <%  if() {} else  %> , 输出参数值使用 <%=title  %>, 有一个 ...

  10. swiper3初始化/swiper-init/用data实例化swiper/data-swiper

    Framework7直接用data属性实例化swiper用起来很爽,刚好最近又用到swiper插件,自己写一个 HTML <div class="swiper-container sw ...