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 ...
随机推荐
- 用python打印99乘法口诀表
代码如下 #!/usr/bin/env python # encoding: utf-8 __author__ = 'Nicholas.Cage' i = 1 j = 1 while i <= ...
- [CF1051F]The Shortest Statement
题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...
- jsp中获取list长度
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ tag ...
- Understanding and Creating OWIN Middlewares - Part 1
In my previous article, What is OWIN? A Beginners Guide we learned the basics of OWIN and the benefi ...
- 爬虫之Fiddler抓取HTTPS设置
Fiddler抓取HTTPS设置 启动Fiddler,打开菜单栏中的 Tools > Telerik Fiddler Options,打开“Fiddler Options”对话框. 对Fiddl ...
- jQuery loop over JSON字符串 – $.each实例
先来一段简单的javascript对象的遍历: var json = [ {"id":"1","tagName":"apple&q ...
- Interleaving String,交叉字符串,动态规划
问题描述: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Give ...
- 如何优雅地使用 rm 防止误删除?
IT 界的有一个老梗,一次某论坛的数据库管理员抱怨自己老板一直虐待他,结果他一气之下就删库跑路了...... 于是... 据新华社北京 8 月 20 日电 ,北京一软件工程师徐某离职后因公司未能如期结 ...
- WebBrowser与console.log()
在WebBrowser中,页面上的console.log()会影响后续代码的执行. <script>console.log(1); alert("1"); //不执行 ...
- Spring Boot技术栈博客笔记(1)
要实现的核心功能 用户管理 安全设置 博客管理 评论管理 点赞管理 分类管理 标签管理 首页搜索 核心技术 数据存储 随着spring3发布以来,spring团队减少使用xml配置的使用,采用大量约定 ...