先上题目:

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

    题意:给你一棵树,树的边是有向的,每条边有一个状态0或者1,现在告诉你根是1,问你最少需要改变多少多少条边的状态才能使每个点到达跟的路径上有一半以上的边是1,并且输出数目和这些边的编号。

    做法:先从根节点dfs一次,对于记录下每个节点还需要多少个1以及这个节点以及它的子树中的节点最多需要1的节点需要1的数目。然后在从根节点再dfs一次,这次的目的是对于当前状态为0的边我们可以考虑是否将它转化为1,因为对于这种转化来说,转移深度小的边对树的影响比转移深度大的边对树的影响更大,所以我们如果一棵子树里面的节点还需要1的话,就转换深度小的边。也就是说这样做符合贪心的思想。

上代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 200002
using namespace std; typedef struct {
int id,u,v,w,next;
} Edge; Edge e[MAX];
char ch[];
int p[MAX],need[MAX],am[MAX];
int tot,top; inline void add(int id,int u,int v,int w) {
e[tot].id=id;
e[tot].u=u;
e[tot].v=v;
e[tot].w=w;
e[tot].next=p[u];
p[u]=tot++;
} void dfs1(int r,int dep,int a) {
am[r]=(dep+)/ - a;
for(int i=p[r]; i!=-; i=e[i].next) {
dfs1(e[i].v,dep+,a+e[i].w);
am[r]=max(am[e[i].v],am[r]);
}
} void dfs2(int r,int num) {
for(int i=p[r]; i!=-; i=e[i].next) {
if(num<am[e[i].v]) {
if(e[i].w==) {
need[top++]=e[i].id;
dfs2(e[i].v,num+);
} else dfs2(e[i].v,num);
}
}
} int main() {
int n,u,v;
//freopen("data.txt","r",stdin);
while(~scanf("%d",&n)) {
memset(p,-,sizeof(p));
tot=top=;
for(int i=; i<n; i++) {
scanf("%d %d %s",&v,&u,ch);
if(ch[]=='a') {
scanf("%s",ch);
add(i,u,v,);
} else {
add(i,u,v,);
}
}
dfs1(,,);
dfs2(,);
printf("%d\n",top);
for(int i=; i<top; i++) {
if(i) printf(" ");
printf("%d",need[i]);
}
printf("\n");
}
return ;
}

/*321*/

SGU - 321 - The Spy Network的更多相关文章

  1. sgu 321 The Spy Network (dfs+贪心)

    321. The Spy Network Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standard ...

  2. SGU 321 知道了双端队列,

    思路: 贪心. 每次删除最上面的边.. #include<utility> #include<iostream> #include<vector> #include ...

  3. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

  4. Tarjan & LCA 套题题目题解

    刷题之前来几套LCA的末班 对于题目 HDU 2586 How far away 2份在线模板第一份倍增,倍增还是比较好理解的 #include <map> #include <se ...

  5. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

  6. SGU 149 Computer Network 树DP/求每个节点最远端长度

    一个比较经典的题型,两次DFS求树上每个点的最远端距离. 参考这里:http://hi.baidu.com/oi_pkqs90/item/914e951c41e7d0ccbf904252 dp[i][ ...

  7. SGU 149. Computer Network

    时间限制:0.25s 空间限制:4M: 题意: 给出一颗n(n<=10000)个节点的树,和n-1条边的长度.求出这棵树每个节点到最远节点的距离: Solution: 对于一个节点,我们可以用D ...

  8. SGU 149 树形DP Computer Network

    这道题搜了一晚上的题解,外加自己想了半个早上,终于想得很透彻了.于是打算好好写一写这题题解,而且这种做法比网上大多数题解要简单而且代码也比较简洁. 首先要把题读懂,把输入读懂,这实际上是一颗有向树.第 ...

  9. COMP 321

    COMP 321April 24, 2019Questions on this exam may refer to the textbook as well as to the manual page ...

随机推荐

  1. P3222 [HNOI2012]射箭

    传送门 黄学长的代码好清楚啊--大概搞明白半平面交是个什么玩意儿了-- 设抛物线 \[y=ax^2+bx\] 则 \[y1<=ax1^2+bx1<=y2\] \[ax1^2+bx1> ...

  2. weiphp插件开发注意

    插件命名要规范,插件名文件名,控制器名,模型名要以大写开头.不然的话会有惊喜!╮(╯▽╰)╭

  3. Nginx(一) 安装基于centos7

    1.   nginx介绍 1.1. 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开 ...

  4. jQuery——表单应用(2)

    多行文本框应用之高度变化 HTML: <!--表单-多行文本框应用-高度变化--> <!DOCTYPE html> <html> <head> < ...

  5. Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局

    Optimizing Layout Hierarchies This lesson teaches you to Inspect Your Layout Revise Your Layout Use ...

  6. 石墨烯(转自wiki)

    石墨烯(Graphene)是一种由碳原子以sp2杂化轨道组成六角型呈蜂巢晶格的平面薄膜,只有一个碳原子厚度的二维材料[1].石墨烯一直被认为是假设性的结构,无法单独稳定存在[1],直至2004年,英国 ...

  7. 分布式爬虫系统设计、实现与实战:爬取京东、苏宁易购全网手机商品数据+MySQL、HBase存储

    http://blog.51cto.com/xpleaf/2093952 1 概述 在不用爬虫框架的情况,经过多方学习,尝试实现了一个分布式爬虫系统,并且可以将数据保存到不同地方,类似MySQL.HB ...

  8. 倒计时和div幻灯片

    倒计时从10秒开始,10秒之后,同意按钮可以使用. 设置一个区域背景,三张照片滚动显示,左右按钮可以下一张上一张,如果点击了左右按钮就不再自动滚动.

  9. from scipy import spatial 出现 from .qhull import * ImportError: DLL load failed: The specified module could not be found. 错误

    错误描述: 本人机器window8.1 64位,python2.7. Traceback (most recent call last): File "C:/Users/Hamid/Docu ...

  10. 本地编译全志R系列的步骤(Ubuntu16.04.4版本)

    本地编译全志R系列的步骤(Ubuntu16.04.4版本) 2018/6/14 9:32 版本:V1.0 0.获取全志R系列的Android源码包: 请通过渠道/代理商/方案公司获取全志R系列的And ...