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. unicode下数据之间的转换

    首先mfc下字符串只有两种数据:char(一个字节)和wchar_t(两个字节),很多其他数据类型如TCHAR,WCHAR等都是这个两个基本类型的宏定义,BYTE是uchar 1.对话框打印char* ...

  2. zabbix3.2通过snmp v2采集Dell服务器iDRAC口信息监控硬件

    模板下载 https://files.cnblogs.com/files/LuckWJL/zbx_export_templates.xml 模板源代码 <?xml version="1 ...

  3. selenium webdriver模拟鼠标键盘

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  4. .Net Core Cookie跨站点共享 会话保持

    这里使用简单粗暴的方式,只为做个记录. 关键配置: services.AddDataProtection() .SetApplicationName("appname") .Dis ...

  5. Ubuntu 安装 networkx

    参考:ubuntu 下NetworkX的安装和使用 Dependences pip setuptools Commands 1.install networkx sudo pip install ne ...

  6. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  7. windows向github提交代码

    随便写的,留给自己看. 一.在github上注册并建立自己的仓库http://www.cnblogs.com/keZhenxu94/p/5288488.html 二.安装windows版本git界面工 ...

  8. nmap 端口扫描王,查看端口是否可访问,是否对外开放

    NMap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. 1 安装 apt-get install nmap 2 使用,查看ip下所有对外的端口 $ sudo nmap ...

  9. 将springboot项目发布到独立的tomcat中运行

    在开发阶段我们推荐使用内嵌的tomcat进行开发,因为这样会方便很多,但是到生成环境,我希望在独立的tomcat容器中运行,因为我们需要对tomcat做额外的优化,这时我们需要将工程打包成war包发进 ...

  10. 可能是最好的 Rx 初学者教程

    译文:https://zhuanlan.zhihu.com/p/25552305 原文:https://gist.github.com/staltz/868e7e9bc2a7b8c1f754