【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());
}
}
}
【Codeforces 369C】 Valera and Elections的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 如何在 ubuntu 12.04 上安装 skype(转载)
转自:http://blog.51osos.com/linux/how-to-install-skype-in-ubuntu-12-04/ 添加Canonical Partner Repository ...
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- Final关键字解析
final 在 Java 中是一个保留的关键字,可以声明变量.方法.类. 什么是final变量 / 类 / 方法? 任何变量前被 final 修饰就是 final 变量,定义的类前被 final 修饰 ...
- 【计蒜客习题】两仪剑法(gcd)
两仪剑法是武当派武功的高级功夫,且必须 2 个人配合使用威力才大.同时该剑法招数变化太快.太多.设武当弟子甲招数变化周期为 n,武当弟子乙招数变化周期为 m,两弟子同时使用该剑法,当 2 人恰好同时达 ...
- 汇编程序49:实验14 访问CMOS RAM(显示系统时间)
assume cs:code ;安装程序,使用指令out和in指令 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax, ...
- [转]Windows Azure平台简介(一):定位与产品结构
本文转自:http://blog.csdn.net/azurechina/article/details/5592236 http://blogs.msdn.com/b/azchina/archive ...
- mysql之命令行导入导出
命令介绍 mysqldump:导出命令,在系统”命令提示符“窗口直接使用,如果提示没有此命令(前提是已经安装成功mysql),在环境变量中的path添加mysql,即path=D:\xxx\mysql ...
- 87. [NOIP2000] 乘积最大
★☆ 输入文件:cjzd.in 输出文件:cjzd.out 简单对比 时间限制:1 s 内存限制:128 MB 问题描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国 ...
- Font Awesome 图标使用总结
参考 http://fontawesome.dashgame.com/ 1 大图标递进 fa-lg (33%递增).fa-2x. fa-3x.fa-4x,或者 fa-5x 2 固定宽度 fa-f ...
- 华硕(ASUS)X554LP笔记本在64位win7下无线网络连接问题
还是那台华硕(ASUS)X554LP笔记本,无线网卡为 Qualcomm Atheros AR956x,某天换了个环境,WIFI(此处简称为WIFI网A)就连不上网了.手机.其它笔记本用无线连接都没问 ...