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 ...
随机推荐
- python 基础 2.6 for 循环 和if循环 中break
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- EasyPlayer RTSP播放器对RTSP播放地址url的通用兼容修改意见
问题反馈 最近在线上遇到一位老朋友咨询关于EasyPlayer播放器的事情,大概现象就是分别用EasyPlayer和vlc播放大华摄像机的RTSP流,流地址是:rtsp://admin:admin12 ...
- 一阶 斜率 二阶 原函数的粗糙度 roughness
1 2 损失函数+惩罚函数 2阶导数
- Java实参和形参与传值和传引用
实参和形参的定义: 形参出现函数定义中,在整个函数体内都可以使用,离开函数则不能使用. 实参出现在主函数中,进入被调函数后,实参变量也不能使用. 形参和实参的功能是做数据传送.发生函数调用时,主调函数 ...
- web项目中从不同的路径读取文件
项目中的配置文件可以放在classpath下,webapp下获取其他任何一个指定的绝对地址,读取这些文件就从这三个地方去找.主要代码如下: private List<String> get ...
- Matlab图像处理(02)-图像基础
数据类 Matlab中和IPT中支持的基本数据类型如下: 名称 描述 double 双精度浮点数,范围-10308~10308 8字节 uint8 无符号1字节整数,范围[0, 255] uint1 ...
- C++之封装
希望暴露public 希望隐藏private 对象实例化有两种方式,从栈实例化,从堆(new出来的)实例化. 以谁做什么作为核心. public 放前面,private放后面(属性可以定义为priva ...
- React之jsx语法特性
jsx 语法,直接可以在js中使用html标签. 还可以通过花括号的形式,在html标签中,写js表达式. <div> { 1 + 2 } hello,world! </div> ...
- legend2---开发日志14(游戏对用户友好的设计思路)
legend2---开发日志14(游戏对用户友好的设计思路) 一.总结 一句话总结: 不强制,但是激励:比如宗门灵力等级从强制提升到提升宗门和用户的修炼速度 1.丹药有必要做成随机数值么? 没有 1. ...
- ES忽略TF-IDF评分——使用constant_score
Ignoring TF/IDF Sometimes we just don’t care about TF/IDF. All we want to know is that a certain wor ...