369C Valera and Elections
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的更多相关文章
- CodeForces - 369C - Valera and Elections
369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...
- 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 ...
- Codeforces 369 C Valera and Elections
Valera and Elections 题意:现在有n个候选人, 有n-1条路, 如果选择了这个候选人, 这个候选人就会将从自己这个城市到1号城市上所有坏的路都修复一下,现在求最小的候选人数目, 如 ...
- 【Codeforces 369C】 Valera and Elections
[链接] 我是链接,点我呀:) [题意] 给你一棵树 让你选择若干个修理点. 这些修理点被选中之后,节点i到1号节点之间的所有"坏路"都会被修好 问最少需要选择多少个点才能将所有的 ...
- cf C. Valera and Elections
http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...
- CF_216_Div_2
比赛链接:http://codeforces.com/contest/369 369C - Valera and Elections: 这是一个树上问题,用深搜,最开始贪心想得是只加叶子节点,找到一个 ...
- Codeforces Round #216 (Div. 2)
以后争取补题不看别人代码,只看思路,今天就是只看思路补完的题,有点小激动. A. Valera and Plates 水题,贪心地先放完第一种食物,在考虑第二种. 居然被卡了一会,心态要蹦 :(: # ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列
A. Bear and Elections ...
- [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 ...
随机推荐
- [NOIP2017]时间复杂度
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- 【网络结构可视化】Visualizing and Understanding Convolutional Networks(ZF-Net) 论文解析
目录 0. 论文地址 1. 概述 2. 可视化结构 2.1 Unpooling 2.2 Rectification: 2.3 Filtering: 3. Feature Visualization 4 ...
- mysql悲观锁以及乐观锁总结和实践
悲观锁介绍(百科): 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持保守态度,因此,在整个数据处理过程中, 将数据处于锁定状态.悲观锁的实现,往往 ...
- nagios配置邮件报警
1.配置sendmail vi /etc/mail.rc 加入以下行 set bsdcompat set from=邮件用户名@domain.com smtp=smtp.126.com set smt ...
- vue的seo方案 prerender-seo-plugin
利用vue cli 3.0安装脚手架.记住:勾选vue-router. 在vue.config.js里添加配置: 2, var path = require('path') 3, const Prer ...
- UIAutomation学习入门
一.界面的自动化操作 .Ui自动化测试 .软件外挂 二.Win32基础知识 a.Win32中一切元素皆窗口,窗口之间有父子关系.整个桌面是一个“根窗口”. b.进程: 根据进程id拿到进程对象Proc ...
- python之Memcached 安装及操作
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- python学习使用
静态变量:函数执行完不释放的变量 python的类中 __init__相当于构造函数.
- python脚本10_打印斐波那契数列的第101项
#打印斐波那契数列的第101项 a = 1 b = 1 for count in range(99): a,b = b,a+b else: print(b) 方法2: #打印斐波那契数列的第101项 ...
- 【Error】IOError: [Errno 22] invalid mode
使用python打开或写入文件时会报以下错误IOError: [Errno 22] invalid mode,比如打开f:\nnpm.txt时,可以在地址前面加上r或R,即r'f:\nnpm.txt' ...