http://codeforces.com/problemset/problem/369/C

树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答案

10w个点要用邻接表存图

#include <iostream>
using namespace std ;
typedef struct L{
int s,t ;
int v ;
int nxt ;
}L ;
L e[] ;
int head[] ;
int cnt ;
void ins(int s,int t,int v)
{
e[cnt].t=t ;
e[cnt].v=(v==) ;
e[cnt].nxt=head[s] ;
head[s]=cnt++ ;
}
int ct ;
int ans[] ;
int vis[] ;
int dfs(int s,int droad)
{
int flag= ;
for(int i=head[s] ;i!=- ;i=e[i].nxt)
{
if(!vis[i])
{
if(i&)
vis[i-]= ;
else
vis[i+]= ;
flag=dfs(e[i].t,e[i].v) || flag ;
}
}
if(droad && flag==)
{
ans[ct++]=s ;
return ;
}
return flag ;
}
void solve()
{
ct= ;
dfs(,) ;
printf("%d\n",ct) ;
for(int i= ;i<ct ;i++)
{
if(!i)
printf("%d",ans[i]) ;
else
printf(" %d",ans[i]) ;
}
putchar('\n') ;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head)) ;
memset(vis,,sizeof(vis)) ;
cnt= ;
for(int i= ;i<n- ;i++)
{
int a,b,c ;
scanf("%d%d%d",&a,&b,&c) ;
ins(a,b,c) ;ins(b,a,c) ;
}
solve() ;
}
return ;
}

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

  1. CodeForces - 369C - Valera and Elections

    369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...

  2. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

  3. Codeforces 369 C Valera and Elections

    Valera and Elections 题意:现在有n个候选人, 有n-1条路, 如果选择了这个候选人, 这个候选人就会将从自己这个城市到1号城市上所有坏的路都修复一下,现在求最小的候选人数目, 如 ...

  4. 【Codeforces 369C】 Valera and Elections

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

  5. cf C. Valera and Elections

    http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...

  6. CF_216_Div_2

    比赛链接:http://codeforces.com/contest/369 369C - Valera and Elections: 这是一个树上问题,用深搜,最开始贪心想得是只加叶子节点,找到一个 ...

  7. Codeforces Round #216 (Div. 2)

    以后争取补题不看别人代码,只看思路,今天就是只看思路补完的题,有点小激动. A. Valera and Plates 水题,贪心地先放完第一种食物,在考虑第二种. 居然被卡了一会,心态要蹦 :(: # ...

  8. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列

                                                    A. Bear and Elections                               ...

  9. [Codeforces Round #237 (Div. 2)] A. Valera and X

    A. Valera and X time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. JS时间和字符串的相互转换 Date+String

    1.js字符串转换成时间 1.1方法一:输入的时间格式为yyyy-MM-dd function convertDateFromString(dateString) { if (dateString) ...

  2. [转载]Javassist 使用指南(一)

    ======================= 本文转载自简书,感谢原作者!. 原链接如下:https://www.jianshu.com/p/43424242846b =============== ...

  3. linux网络连接的查看和端口的监听

    网络软件都是由客户端和服务端组成,由服务端在服务器上监听指定的端口处理接收到的数据,而客户端是向服务器端监听的端口发送数据,并由服务器端对该数据进行处理,然后将处理结果返回给客户端. 那么我们在lin ...

  4. dp之最长上升子序列

    普通做法是O(n^2)下面介绍:最长上升子序列O(nlogn)算法(http://blog.csdn.net/shuangde800/article/details/7474903) /* HDU 1 ...

  5. Linux升级内核总结

    Linux内核升级总结. 一.编译内核步骤 1.#uname –r 确定系统的原内核版本,然后下载较新版本的Linux内核源码包 http://www.kernel.org/pub/linux/ker ...

  6. replace()函数用法

    replace()函数表示将用一个字符串替换字符串中的所出现的特定内容. 语法为:replace(字段1,字段2,字段3),意思为字段3将会替换字段1里与字段2相同的内容  列如: table1 st ...

  7. web常见问题排查

    原帖地址:http://mp.weixin.qq.com/s?__biz=MjM5NzUwNDA5MA==&mid=200596752&idx=1&sn=37ecae802f3 ...

  8. [洛谷U62358]求导函数

    U62358 求导函数 题面 给出一个n次函数\(f(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+...+a_{1}x+a_0\)的各项系数\(a_n,a_{n-1}...a_1,a_0 ...

  9. HDU1595-最短路-删边

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  10. sql 多个字段分组,删除重复记录,保留ID最小的一条

    IF OBJECT_ID('cardDetail') IS NOT NULL DROP TABLE cardDetail CREATE TABLE cardDetail ( id ,) PRIMARY ...