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

【题意】

给你一棵树
让你选择若干个修理点.
这些修理点被选中之后,节点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. bzoj 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱【区间dp】

    就是区间dp啦f[i][j]表示以i开头的长为j+1的一段的答案,转移是f[i][j]=s[i+l]-s[i-1]+min(f[i][j-1],f[i+1][j-1]),初始是f[i][1]=a[i] ...

  2. java 实现word文档在线预览

    一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...

  3. golang——字符串与编码

    1.字符编码 (1)ASCII码 一个字节表示的英文.数字.标点符号等字符. 国际标准ASCII码为0-127即128个字符,二进制最高位为0,其余为扩展ASCII码. (2)GB2312 两字节,主 ...

  4. Java多线程(八) synchronized 抛出异常锁自动解除

    当一个线程执行的代码出现异常时,其所持有的锁会自动释放 public class MyObject { private int i = 1; synchronized public void meth ...

  5. 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)

    Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...

  6. ViewPager(1)FragmentPagerAdapter

    FragmentPagerAdapter 适合只有少量的pager,所有pager同时全部存在,不会有被销毁的,page过多很容易内存溢出. 1,代码 1.1 ViewPagerMain.java i ...

  7. 计算科学(转自wiki)

    计算科学(也称科学计算 scientific computation 或 SC)是一个快速增长的多学科领域,使用先进的计算能力来理解和解决复杂的问题. 计算科学包括三个不同的方面: 1. 开发用于解决 ...

  8. BST二叉查找树转双向链表DoubleLinke

    问题:在不创建任何新的节点的情况下,实现将一颗BST变成有序的双向链表. 分析: 在结构上,如图的一颗BST,每个节点都有left right指针分别指指向左右儿子.结构上和双向链表节点是完全相同的. ...

  9. [译]libcurl错误码

    CURLcode Almost all "easy" interface functions return a CURLcode error code. No matter wha ...

  10. jvm内存分区

    java内存是由jvm进行管理的,其内存简易模型如下图: jvm管理的内存大体上可分为方法区.堆.程序计数器.线程栈.本地方法区这几部分.方法区:主要存放类的元信息(包括类的名称.修饰符.静态变量.f ...