枪战(maf)

settle the dispute. Negotiations were very tense, and at one point the trigger-happy participants drew their

guns. Each participant aims at another with a pistol. Should they go on a killing spree, the shooting will go

in accordance with the following code of honour:

the participants shoot in a certain order, and at any moment at most one of them is shooting,

no shooter misses, his target dies instantly, hence he may not shoot afterwards,

everyone shoots once, provided he had not been shot before he has a chance to shoot,

no participant may change his first target of choice, even if the target is already dead (then the shot

causes no further casualties).

An undertaker watches from afar, as he usually does. After all, the mobsters have never failed to stimulate

his business. He sees potential profit in the shooting, but he would like to know tight estimations. Precisely

he would like to know the minimum and maximum possible death rate. The undertaker sees who aims at

whom, but does not know the order of shooting. You are to write a programme that determines the numbers

reads from the standard input what target each mobster has chosen,

determines the minimum and maximum number of casualties,

输入

第1行:人数 1<=n<=1,000,000

第2行:包含了n 个整数s1,s2,...,sn,si表示第i个人瞄准的目标,注意一下第i个的目标可能为i 。

输出

输出2个整数。枪战后可能的最小和最大的死亡人数。


solution

考虑贪心。

首先没有入度的点一定不会死。假设为A

那么他们瞄准的人B一定会死,而B瞄准的人可能可以不死。

于是删除B的出边,类似拓扑序,一个点如果没有入边,它就可以不死。

还剩下若干环。

环上的点最少死n/2

最多:

1.如果这个环有入边(就是它不是一个单独的环,环外可以打到环上)

那么可以全死。因为让那个被环外打的人开枪

不然会活一个人

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 1000006
using namespace std;
int n,a[maxn],in[maxn];
int q[maxn],l,r,ans1,ans2,die[maxn];//ans1 min ans2 max
int flag[maxn];
int main()
{
cin>>n;
for(int i=1;i<=n;i++){scanf("%d",&a[i]);in[a[i]]++;}
for(int i=1;i<=n;i++){
if(!in[i])q[++r]=i,ans1++;
}
for(int i=1;i<=r;i++){
int u=q[i];int v=a[u];
if(die[v])continue;
die[v]=1;
in[a[v]]--;flag[a[v]]=1;// flag=1 exist a entrance
if(!in[a[v]])q[++r]=a[v];
}
ans2=r;
for(int i=1;i<=n;i++){
if(in[i]&&!die[i]){// in the circle
int len=0,en=0;
for(int j=i;!die[j];j=a[j]){
die[j]=1;len++;
en|=flag[j];
}
if(!en&&len>1)ans1++;//can't enter
ans2+=len/2;
}
}
printf("%d %d\n",n-ans2,n-ans1);
return 0;
}

枪战(maf)的更多相关文章

  1. BZOJ 1124: [POI2008]枪战Maf

    1124: [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 617  Solved: 236[Submit][Status ...

  2. [POI2008]枪战Maf

    [POI2008]枪战Maf 题目 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的 ...

  3. [BZOJ 1124][POI 2008] 枪战 Maf

    1124: [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 659  Solved: 259[Submit][Status ...

  4. bzoj 1124 [POI2008]枪战Maf 贪心

    [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 741  Solved: 295[Submit][Status][Disc ...

  5. 【BZOJ1124】[POI2008]枪战Maf 贪心+思路题

    [BZOJ1124][POI2008]枪战Maf Description 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开 ...

  6. [POI2008]枪战Maf题解

    问题 C: [POI2008]枪战Maf 时间限制: 1 Sec  内存限制: 256 MB 题目描述 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺 ...

  7. BZOJ1124 [POI2008]枪战Maf[贪心(证明未完成)+拓扑排序]

    吐槽:扣了几个小时,大致思路是有了,但是贪心的证明就是不会, 死磕了很长时间,不想想了,结果码代码又不会码.. 深深体会到自己码力很差,写很多行还没写对,最后别人代码全一二十行,要哭了 以下可能是个人 ...

  8. 枪战Maf[POI2008]

    题目描述 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. 输入 输入n人 ...

  9. bzoj1124[POI2008]枪战maf

    这代码快写死我了.....死人最多随便推推结论.死人最少,每个环可以单独考虑,每个环上挂着的每棵树也可以分别考虑.tarjan找出所有环,对环上每个点,求出选它和不选它时以它为根的树的最大独立集(就是 ...

  10. 【BZOJ】1124: [POI2008]枪战Maf

    题意 \(n(n < 1000000)\)个人,每个人\(i\)指向一个人\(p_i\),如果轮到\(i\)了且他没死,则他会将\(p_i\)打死.求一种顺序,问死的人最少和最多的数目. 分析 ...

随机推荐

  1. Redis高可用

    redis高可用只要在于三个方面 主从复制 哨兵机制 集群机制 主从复制 主从复制作用: 1.数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式.2.故障恢复:当主节点出现问题时,可 ...

  2. 转 MYSQL 命令行大全 (简洁、明了、全面)

    MYSQL常用命令 .导出整个数据库 mysqldump -u 用户名 -p –default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1 ...

  3. struts2入门第一天----------配置环境

    放假之后有空就开始走上了三大框架的学习.第一个选择的框架是struts2.首先第一步当然是环境的配置.去apache官网把struts2下载下来.然后在自己的开发工具下创建一个web项目.在lib文件 ...

  4. linux下csv导出文件中文乱码问题

    近日在服务器端通过导出csv文件,将数据从linux服务器端保存到windows桌面端,以便用户可以通过excel打开使用数据. 但是在使用excel打开csv文件时,出现了中文乱码的情况,但是使用记 ...

  5. nuxt.config有关router配置

    这里只说明一个属性,其他属性移步官方文档 https://zh.nuxtjs.org/api/configuration-router extendRoutes   官方说明: 你可以通过 exten ...

  6. 关于 PHP 程序员技术职业生涯规划

    原文地址:http://rango.swoole.com/archives/570 看到很多 PHP 程序员职业规划的文章,都是直接上来就提 Linux.PHP.MySQL.Nginx.Redis.M ...

  7. 数据库DDL

    自己对数据库的整理,也是对自己知识的梳理 SQL ( Structure query language ) 结构化查询语言 SQL语言分为4个部分 1.DDL(Data Definition Lang ...

  8. 浅谈XX系统跨平台迁移(测试环境)

    一 概述 XX系统目前运行在XX-A的云平台上,计划将其迁移至XX-B的云平台. XX系统是java开发,中间组件涉及nginx+keepalived实现各个业务系统之间的高可用,kafka,zook ...

  9. PHP.23-ThinkPHP框架的三种模型实例化-(D()方法与M()方法的区别)

    三种模型实例化 原则上:每个数据表应对应一个模型类(Home/Model/GoodsModel.class.php --> 表tp_goods) 1.直接实例化 和实例化其他类库一样实例化模型类 ...

  10. Spring---加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplicati ...