cf C. Valera and Elections
http://codeforces.com/contest/369/problem/C
先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还是有问题的边,这个点就是所求的点。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 200000
using namespace std; int head[maxn],e,cnt,n;
int ans[maxn];
bool vis[maxn];
int point[maxn];
struct node
{
int u,v,w;
int next;
} p[maxn]; void add(int u,int v,int w)
{
p[e].u=u;
p[e].v=v;
p[e].w=w;
p[e].next=head[u];
head[u]=e++;
} void cls()
{
memset(head,-,sizeof(head));
memset(vis,false,sizeof(vis));
memset(ans,,sizeof(ans));
e=;
cnt=;
} int dfs(int u,int flag)
{
vis[u]=;
for(int i=head[u]; i!=-; i=p[i].next)
{
int v=p[i].v,w=p[i].w;
if(!vis[v])
{
ans[u]+=dfs(v,w);
}
}
if(flag==||ans[u]>=)
{
if(ans[u]==)
point[cnt++]=u;
return ;
}
return ;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cls();
for(int i=; i<=n-; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(,);
printf("%d\n",cnt);
for(int i=; i<cnt; i++)
{
if(i==) printf("%d",point[i]);
else printf(" %d",point[i]);
}
printf("\n");
}
return ;
}
cf C. Valera and Elections的更多相关文章
- 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 - 369C - Valera and Elections
369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...
- Codeforces 369 C Valera and Elections
Valera and Elections 题意:现在有n个候选人, 有n-1条路, 如果选择了这个候选人, 这个候选人就会将从自己这个城市到1号城市上所有坏的路都修复一下,现在求最小的候选人数目, 如 ...
- CF 441E Valera and Number
CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...
- cf E. Valera and Queries
http://codeforces.com/contest/369/problem/E 题意:输入n,m; n 代表有多少个线段,m代表有多少个询问点集.每一个询问输出这些点的集合所占的线段的个数. ...
- cf D. Valera and Fools
http://codeforces.com/contest/369/problem/D 标号最小的两个人会有四种状态:a活b活,a死b活,a活b死,a死b死:按照这四种状态dfs就可以求出最后的数量. ...
- cf B. Valera and Contest
http://codeforces.com/contest/369/problem/B 先对k个处理,先处理sk%k个为sk/k+1,如果sk/k==0,k个数都为sk/k:对与剩下的数也按照同样的方 ...
- CF(441D Valera and Swaps)置换群
题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...
- 369C Valera and Elections
http://codeforces.com/problemset/problem/369/C 树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答 ...
随机推荐
- android ids.xml资源的使用
ids.xml文件例子: XML file saved at res/values/ids.xml: 使用方式: 一:
- 一条sql
包含全部:SELECT u.id,u.user_name,usa.fixed_amount,d.uloadtype,d.min_loan_money FROM fanwe_user_autobid u ...
- arcpagelistarclist列表分页
arcpagelistarclist列表分页 (DedeCMS 5.6) 名称:arcpagelist 功能:通过制定arclist的pagesize及tagid属性,配合arcpagelist标签进 ...
- javascript 知识点坑
1. JavaScript事件属性 event.target 当目标事件发生span里面 当目标事件发生在main里面 e.target; // 目标节点DOM结构 e.target.id; // ...
- Linux命令 rpm
rpm -q samba --查询程序是否安装rpm -qa | grep httpd --[搜索指定rpm包是否安装] --all搜索*http ...
- OpenSuse下编译MonoDevelop
当访问Monodevelop.com官网下载的安装包,安装后,发现并不是最新版.在OpenSuse下载的是3.0版本.根据官网的指示,可以自己下载源码进行编译.按官网的指引: 1. $ git clo ...
- javascript 实现jsonp
jsonp原理其实也简单,虽然ajax不能跨域,但是通过src这个属性我们可以实现跨域,其实和我们引入第三方jquery调用它的方法一样的. html: <!DOCTYPE html> & ...
- 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey>
转自:http://hi.baidu.com/taotaowyx/blog/item/074bb8d83907bb3233fa1ce6.html 验证视图状态 MAC 失败.如果此应用程序由网络场或群 ...
- (转)使用Microsoft Web Application Stress Tool对web进行压力测试
http://www.blogjava.net/crespochen/archive/2009/06/02/279538.html Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试 ...
- 纯js滑动脚本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...