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. rocketmq事务消息

    rocketmq事务消息 参考: https://blog.csdn.net/u011686226/article/details/78106215 https://yq.aliyun.com/art ...

  2. 【bzoj5177】[Jsoi2013]贪心的导游(分块)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5177 在网上看到的题解基本都是用主席树,也就是带点骚操作的暴力直接艹过去的.这里分享一 ...

  3. Stm32F103面向对象编程之GPIO

    STM32F103 GPIO面向对象变成实例 创建一个GPIO对象 GpioClass gStateLed = NEW_GPIO(C, 0, GPIO_Mode_Out_PP); 初始化对象 Gpio ...

  4. springMVC数据回显

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  5. 03_MySQL DQL_排序查询

    #进阶3:排序查询/*语法: select 查询列表 from 表名 [where 筛选条件] order by 排序列表 [asc|desc] 特点: 1.asc升序,desc降序, 如果都不写,默 ...

  6. Asp.Net MVC向视图View传值的三种方法

    本文将总结Asp.Net MVC向视图View传值的三种常见的方法: ----------------------------------------------------------------- ...

  7. Gtk基础学习总结(一)

    第一个GTK程序例子: #include <stdio.h> #include <gtk/gtk.h> int main(int argc, char *argv[]) { g ...

  8. MySQL行锁、间隙锁、Next-Key锁

    InnoDB是一个支持行锁的存储引擎,它有三种行锁的算法: Record Lock:行锁,单个行记录上的锁. Gap Lock:间隙锁,锁定一个范围,但不包括记录本身.GAP锁的目的,是为了防止幻读. ...

  9. Exception occurred during processing request: null报错

    报错, 恶心的一笔. 报错的地方 解决方法: 没注意到...

  10. angularjs控制器之间的数据共享与通信

    1.可以写一个service服务,从而达到数据和代码的共享; var app=angular.module('app',[]); app.service('ObjectService', [Objec ...