【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- node.js开发错误——DeprecationWarning: Mongoose: mpromise
原文地址 使用mongoose进行数据库操作时,总是提示: (node:5684) DeprecationWarning: Mongoose: mpromise (mongoose's default ...
- /bin/bash: jar: command not found(转载)
转自:http://blog.csdn.net/zhangdaiscott/article/details/23138023 /bin/bash: jar: command not found 解决办 ...
- Django day35 redis连接池,redis-list操作,django中使用redis,支付宝支付
一:redis连接池, 二:redis-list操作, 三:django中使用redis, 四:支付宝支付
- Visual C++ Windows 桌面应用程序样例(摘抄)
//================================== //Windows应用程序框架结构(例子) //参考:<Visual C++宝典>陈国建等编著 //======= ...
- [Android]异常3-java.lang.NoClassDefFoundError: javax.activation.DataHandler
背景:JavaMail发送电子邮件 异常原因: 可能一>缺少DataHandler类相关jar包 可能二>有DataHandler类,DataHandler类与使用的mail.jar包不一 ...
- Vue.js——router-link阻止click事件
router-link 只能单纯做路由跳转 https://segmentfault.com/q/1010000007896386
- ElasticSearch学习笔记--安装
1.安装ElasticSearch https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html 这 ...
- cookie、json详解
什么是cookie 1.cookie是存储于访问者计算机中的变量2.cookie是浏览器提供的一种机制3.可以由js控制(设置.读取.删除)4.cookie可以实现跨页面全局变量可以跨越同域名下多个网 ...
- interface与抽象类
抽象类: 定义:在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类 抽象类不能被实例化. 抽象类被继承之后,子类必 ...
- 平凡的世界小说txt下载完整版
<平凡的世界>正白纸黑字的告诉我们这样的人生真谛.它响亮的提出,人,无论在什么位置,无论多么贫寒,只要一颗火热的心在,只要能热爱生活,上帝对他就是平等的.只有作一名劳动者,不把不幸当作负担 ...