sgu 321 The Spy Network (dfs+贪心)
321. The Spy Network
Memory limit: 65536 kilobytes
output: standard
The network of spies consists of N intelligence officers. They are numbered with the code numbers from 1 to N so that nobody could discover them. The number 1 belongs to the radiowoman Kat. There is exactly N - 1 communication channels between the spies. It is known that a message from any spy to Kat can reach her. All channels are unidirectional.
A channel can have one of two types: protected and almost protected. It is known that a message will not be intercepted almost surely by the hostile security service if at least half of the channels along the path to radiowoman Kat are protected. What is the minimum number of channels to be made protected from almost protected, so that any message from any spy will not be intercepted almost surely ? What are those channels?
The first line of the input contains the integer number N (1 ≤ N ≤ 200000). The following N - 1 lines contain the description of the communication channels. Each channel is described by a pair of the code numbers of spies (the direction of the channel is from the first spy to the second one) and the parameter pi. If pi =
protected
, the channel is protected and if pi =
almost protected
, the channel is almost protected.
Write the number of channels to be converted to protected to the first line of the output. To the next line write numbers of channels to be made protected. If there are several solutions, choose any of them.
sample input |
sample output |
5 |
2 |
题意:一棵结点数为n的树,给出n-1条边,每条边有一个属性,0(almost protect)和1(protect),修改某些边的属性使得满足树上每一个结点到根节点的路径上,属性为1的边的数量大于等于属性为0的边的数量,求最小操作数
思路:首先要修改的边,应该离根节点越近越好,越近的话,对其他结点的贡献越多。所以用dfs(u)表示u的所有子节点中最多需要的1的边的数量,当这个数量大于u的深度的时候,就表示u前面全部修改了也不够满足条件,这时候就看u和下一个结点连接的边的属性是1还是0,是0的话,就要修改这条边,加进答案里面。dfs扫一遍就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <utility>
#include <queue>
#include <stack>
using namespace std;
const int INF=<<;
const double eps=1e-;
const int N = ; struct _edge{
int u,v,next;
bool w;
};
_edge e[N];
int first[N],n;
vector<int> ans; int dfs(int u,int dep)
{
int res = (dep+)/;
for(int i=first[u];i!=-;i=e[i].next)
{
int v = e[i].v;
int tmp = dfs(v,dep+);
if(e[i].w) tmp--;
else if(tmp>dep)
{
ans.push_back(i);
tmp--;
}
res = max(res,tmp);
}
return res;
} void run()
{
int u,v;
char s[];
memset(first,-,sizeof(first));
for(int i=;i<n;i++)
{
scanf("%d%d",&v,&u);
e[i].u = u;
e[i].v = v;
scanf("%s",s);
if(s[]=='a')
e[i].w=,scanf("%s",s);
else
e[i].w=;
e[i].next = first[u];
first[u] = i;
}
// for(int i=1;i<n;i++)
// cout<<e[i].u << ' ' << e[i].v << ' ' << e[i].w<<endl;
ans.clear();
dfs(,);
printf("%d\n",ans.size());
if(!ans.empty()) printf("%d",ans[]);
for(int i=;i<ans.size();i++)
printf(" %d",ans[i]);
puts("");
} int main()
{
freopen("case.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
run();
return ;
}
sgu 321 The Spy Network (dfs+贪心)的更多相关文章
- SGU - 321 - The Spy Network
先上题目: 321. The Spy Network Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)
题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...
- 【NOIP2015】斗地主 题解(DFS+贪心)
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
- NOIP2015斗地主[DFS 贪心]
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- [poj2349]Arctic Network(最小生成树+贪心)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17758 Accepted: 5646 D ...
- NOIP2010引水入城[BFS DFS 贪心]
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- Aizu 2302 On or Off dfs/贪心
On or Off Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...
随机推荐
- java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000
在操作SQL中存在In的数量如果超过1000条会提示 ORA-01795: 列表中的最大表达式数为 1000 归纳有几种方式出现的: 第一种是:我在上一个 [jdbc 同时执行 查询和删除操]作中 ...
- go test 下篇
前言 go test 上篇 给大家介绍了golang自带的测试框架,包括单元测试和性能测试.但是在实际生产中测试经常会遇到一些网络或者依赖的第三方系统接口,运行测试用例的时候希望忽略这些接口的实际依赖 ...
- tomcat+java 占cpu 调试【top命令应用】
原文出处:http://www.blogjava.net/hankchen 现象: 在tomcat中部署java的web应用程序,过一段时间后出现tomcat的java进程持续占用cpu高达100%, ...
- MapReduce-PRODUCTION-DEMAND
[粗暴的HIVE-SQL]select xyz from abc where ty='sdk' and ret_code=0 and data_source_type=1 and dt between ...
- [持续集成]Jenkins 自动化部署 Maven 工程
一.Jenkins 持续部署原理图 基础服务: 1 SVN 服务 SVN是Subversion的简称,是一个开放源代码的版本控制系统.说得简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的 ...
- ruby 正则表达式
Ruby学习笔记-正则表达式 Posted on 2011-11-29 17:55 Glen He 阅读(4998) 评论(0) 编辑 收藏 1.创建正则表达式 a) reg1 = /^[a-z]*$ ...
- jQuery的事件绑定和解除
1 . 绑定事件 语法 : bind(type,data,fn) 描述 : 为每一个匹配的特定元素(像 click)绑定一个事件处理器函数. type(String) : 事件类型 data(Obje ...
- 如何使用安信可 ESP 系列一体化开发环境【转】
本文转载自:http://wiki.ai-thinker.com/ai_ide_use 关于 Problems 报错 注意:Eclipse 只是一个代码编写工具,它并不能读取 makefile 里面的 ...
- 【html学习整理】meta,img,表格,表单
meta标签: 作用: 给搜索引擎用 . 告诉浏览器是什么编码 <meta charset="UTF-8"> <meta name="keywords& ...
- sqlite:多线程操作数据库“database is locked”解决方法
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...