321. The Spy Network

Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
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?

Input

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.

Output

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.

Example(s)
sample input
sample output
5
5 1 almost protected
3 1 almost protected
2 3 protected
4 3 almost protected
2
1 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+贪心)的更多相关文章

  1. SGU - 321 - The Spy Network

    先上题目: 321. The Spy Network Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...

  2. 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心

    题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...

  3. 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 ...

  4. 【NOIP2015】斗地主 题解(DFS+贪心)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的AAA到KKK加上大小王的共545454张牌来进行的扑克牌游戏.在斗地主中,牌的大小关 系根据牌的数码表示如下: ...

  5. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

  6. NOIP2015斗地主[DFS 贪心]

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  7. [poj2349]Arctic Network(最小生成树+贪心)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17758   Accepted: 5646 D ...

  8. NOIP2010引水入城[BFS DFS 贪心]

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  9. 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 ...

随机推荐

  1. c++ 通过数据流方式实现文件拷贝

    #include "stdafx.h"#include <iostream>#include<fstream>using namespace std;voi ...

  2. SD相关的表

    [转] 一.客户主数据基本数据放在KNA1里:公司代码放在KNB1里:销售视图放在KNVV里:合作伙伴放在KNVP里:二.信用主数据KNKK里有信贷限额.应收总额.特别往来:S066里是未清订单值:S ...

  3. 常见C C++问题(转)

    这一部分是C/C++程序员在面试的时候会被问到的一些题目的汇总.来源于基本笔试面试书籍,可能有一部分题比较老,但是这也算是基础中的基础,就归纳归纳放上来了.大牛们看到一笑而过就好,普通人看看要是能补上 ...

  4. uboot 2013.01 代码简析(2)第一阶段初始化

    uboot执行"make smdk2410_config"之后就可以进行编译了,可以执行make命令进行编译, 因为整个输出太长,我仅仅列出部分最关键的输出(部分我不关心的内容直接 ...

  5. Codeforces 872C Maximum splitting:数学【分解成合数之和】

    题目链接:http://codeforces.com/contest/872/problem/C 题意: 给你一个数n,问你最多能将n分解成多少个合数之和.(若不能分解,输出-1) 题解: 若要让合数 ...

  6. BZOJ 3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者:贪心【最多线段覆盖】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1324 题意: 给你n个区间,问你最多能选择多少个区间使得它们不相互覆盖. 题解: RQ ...

  7. ES 搜索结果expalain 可以类似数据库性能调优来看排序算法的选择

    When we run a simple term query with explain set to true (see Understanding the Score), you will see ...

  8. openfire插件开发环境

    创建java工程 SamplePlugin: package com.hoo.server.plugin; import java.io.File; import org.jivesoftware.o ...

  9. Mybatis-Generator_学习_02_使用Mapper专用的MyBatis Generator插件

    源码见:https://github.com/shirayner/tk-mybatis-generator 一.要点 二.具体实现 1.项目结构 2.配置 pm.xml <?xml versio ...

  10. tomcat 6.0 安装及配置

    前提:安装并配置好jdk 1.免安装版存放目录:D:\01Install\tomcat