先上题目:

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*/

最新文章

  1. EventDispatcher 事件分发组件
  2. RabbitMQ的几种应用场景
  3. svg绘制圆弧
  4. 赴美工作常识(Part 6 - 绿卡排队)
  5. Namenode主节点停止报错 Error: flush failed for required journal
  6. css3 calc()方法详解
  7. [转]reids客户端 redis-cli用法
  8. C4.5决策树算法概念学习
  9. MVC+Ef项目(4) 抽象业务逻辑层BLL层
  10. bzoj 1034 (田忌赛马++)
  11. Java通过axis调用.NET WebService
  12. UVa 10330 - Power Transmission(最大流--拆点)
  13. 解决xcode7.3的一个bug的方法
  14. HTTP长连接、短连接使用及测试
  15. 基于maven的spring-boot的pom文件详解
  16. Flask对数据库的操作-----
  17. 02MySQL中的数据类型
  18. js生成条形码插件
  19. SQL2008配置管理工具服务显示远程过程调用失败
  20. 图片上传--Upload

热门文章

  1. bzoj4987: Tree(树形dp)
  2. js 本地存储 localStorage 之 angular
  3. 【Linux】小米路由开启SSH访问权限
  4. phpAnalysis调试接口
  5. 【Nodejs】记一次图像识别的冒险
  6. 递推DP UVA 1424 Salesmen
  7. 274 H-Index H指数
  8. Hbase源码分析:server端RPC
  9. excel poi 取单元格的值
  10. LN : leetcode 406 Queue Reconstruction by Height