[POI2008]MAF-Mafia(图论,贪心)
题目描述
Mob feud rages in Equatorial Byteotia. The mob bosses have come to the country's capital, Byteburg, to 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 he is so keen to know.
Task Write a programme that:
reads from the standard input what target each mobster has chosen, determines the minimum and maximum number of casualties, writes out the result to the standard output.
给定n个神枪手,每个神枪手瞄准一个人,以一定顺序开枪,问最少和最多死多少人
输入输出格式
输入格式:
The first line of the standard input contains the number of participants (
).
They are numbered from to
.
The second line contains integers
, separated by single spaces,
.
denotes the number of
participant's target.
Note that it is possible that for some
(the nerves, you know).
输出格式:
Your programme should write out two integers separated by a
single space in the first and only line of the standard output. These
numbers should be, respectively, the minimum and maximum number of
casualties resulting from the shooting.
思路:
入度为0的人肯定死不了,跳过
自杀的救不了,跳过
剩下的就是一个仙人掌
对于一个独立的大小为n的环,最后至多剩下n/2个人,最少剩下一个人
对于一个独立的树,根肯定死不了然后就是个树上问题
环套数缩点瞎搞就行
代码:
#include<iostream>
#include<cstdio>
#include<stack>
#define rii register int i
using namespace std;
int n,to[],e,low[],c[],size[],a1,dfn[];
int a2,xt[],rh[],maxn,minx,rd[];
stack<int> q;
bool r[],zh[],ww[],ded[];
int bj(int x,int y)
{
if(x>=y)
{
x=y;
}
}
void tarjan(int x)
{
e++;
low[x]=dfn[x]=e;
r[x]=;
q.push(x);
if(!dfn[to[x]])
{
tarjan(to[x]);
low[x]=bj(low[x],low[to[x]]);
}
else
{
if(r[to[x]])
{
low[x]=bj(low[x],dfn[to[x]]);
} }
if(low[x]==dfn[x])
{
a1++;
do
{
a2=q.top();
q.pop();
r[a2]=;
c[a2]=a1;
size[a1]++;
}while(a2!=x);
}
}
int main()
{
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d",&to[i]);
if(to[i]==i)
{
minx++;
maxn++;
ded[i]=;
}
rd[to[i]]++;
}
for(rii=;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
while(!q.empty())
{
q.pop();
}
for(rii=;i<=n;i++)
{
if(c[i]!=c[to[i]])
{
xt[c[i]]=c[to[i]];
rh[c[to[i]]]++;
}
if(i==to[i])
{
zh[c[i]]=;
}
}
for(rii=;i<=n;i++)
{
if(!rd[i])
{
q.push(i);
}
}
while(!q.empty())
{
a2=q.top();
ww[c[a2]]=;
q.pop();
ww[c[to[a2]]]=;
if(!ded[to[a2]])
{
minx++;
ded[to[a2]]=;
a2=to[to[a2]];
rd[a2]--;
if(rd[a2]==&&!ded[a2])
{
q.push(a2);
}
}
}
for(rii=;i<=a1;i++)
{
if(size[i]!=&&!ww[i])
{
if(size[i]&)
{
a2=(size[i]+)>>;
}
else
{
a2=size[i]>>;
}
minx+=a2;
}
if(size[i]!=&&!rh[i])
{
maxn+=size[i]-;
}
if(size[i]!=&&rh[i])
{
maxn+=size[i];
}
if(size[i]==&&rh[i]&&!zh[i])
{
maxn++;
}
}
printf("%d %d",minx,maxn);
}
[POI2008]MAF-Mafia(图论,贪心)的更多相关文章
- BZOJ 1124: [POI2008]枪战Maf(构造 + 贪心)
题意 有 \(n\) 个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪. 因此,对于不同的开枪顺序,最后死的人也不同. 问最 ...
- Ant Man CodeForces - 704B (图论,贪心)
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| ...
- bzoj 1122 [POI2008]账本BBB 模拟贪心,单调队列
[POI2008]账本BBB Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 524 Solved: 251[Submit][Status][Disc ...
- 雅礼培训 Problem B 【图论 + 贪心】
题意 A和B在树上轮流选点,记A的联通块个数为\(x\),B的联通块个数为\(y\) A使\(x - y\)最大,B使\(x - y\) 二人采取最优策略,求\(x-y\) 题解 树联通块个数 = 点 ...
- [CSP-S模拟测试]:Graph(图论+贪心)
题目描述 给定一张$n$个点$m$条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通你想在这张图上进行若干次旅游,每次旅游可以任选一个点$x$作为起点,再走到一个与 $x$直接有边相连的点$ ...
- Codeforces 553D Nudist Beach(图论,贪心)
Solution: 假设已经选了所有的点. 如果从中删掉一个点,那么其它所有点的分值只可能减少或者不变. 如果要使若干步删除后最小的分值变大,那么删掉的点集中肯定要包含当前分值最小的点. 所以每次删掉 ...
- 洛谷P1983 车站分级
P1983 车站分级 297通过 1.1K提交 题目提供者该用户不存在 标签图论贪心NOIp普及组2013 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 求帮忙指出问题! 我这么和(diao ...
- 洛谷P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...
- ACM训练小结-2018年6月19日
今天题目情况如下: A题:考察图论建模+判割点.B题:考察基础数据结构的运用(STL).C题:考察数学建模+运算.(三分可解)D题:考察读题+建模+数据结构的运用.E题:考察图论+贪心.F题:考察图 ...
随机推荐
- 2018-12-20 第二章Java 预习作业
一.什么是变量?如何使用变量? 答:变量是在程序运行中其值可以改变的量,它是Java程序的一个基本存储单元. 变量的语法格式如下: [访问修饰符]变量类型 变量名[=初始值]: 二.java中基本数据 ...
- Django之(URL)路由系统
路由系统 简而言之,django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里的关系条目,去查找到与请求对应的处理方法,从而返回给客户 ...
- HashMap和Hashtable的比较
相同点 HashMap和Hashtable都是存储“键值对(key-value)”的散列表,而且都是采用拉链法解决hash冲突的.但是1.8中,hashmap引入了红黑树.Hashtable没有引入红 ...
- Web安全色的意义
问题: 不同的平台(Mac.PC等)有不同的调色板,不同的浏览器也有自己的调色板.这就意味着对于一幅图,显示在Mac上的Web浏览器中的图像,与它在PC上相同浏览器中显示的效果可能差别很大. 选择特定 ...
- js数组的sort排序的原理和应用
1.js sort()方法的应用: 首先:如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串( ...
- Polly一种.NET弹性和瞬态故障处理库(重试策略、断路器、超时、隔板隔离、缓存、回退、策略包装)
下载地址:https://github.com/App-vNext/Polly 该库实现了七种恢复策略. 重试策略(Retry) 重试策略针对的前置条件是短暂的故障延迟且在短暂的延迟之后能够自我纠正. ...
- Unity 多个Android sdk 插件如何组织目录
一般Android 插件放在 Assets/Plugins/Android/ 下, 但是一个项目可能要用到多个sdk , 比如既要用 阿里九游的sdk 又要用 share sdk 怎么办呢, 难道要只 ...
- 【NLP_Stanford课堂】句子切分
依照什么切分句子——标点符号 无歧义的:!?等 存在歧义的:. 英文中的.不止表示句号,也可能出现在句子中间,比如缩写Dr. 或者数字里的小数点4.3 解决方法:建立一个二元分类器: 检查“.” 判断 ...
- 再学UML-Bug管理系统UML2.0建模实例(四)
3.3 顺序图(实现模型) 在系统设计与实现阶段我们也可以使用顺序图进行建模,此时通过顺序图可以明确表示系统设计中对象之间的交互,考虑到具体系统实现,对象之间通过方法调用传递消息.在BMS系统中,对每 ...
- IOS UIDevice距离传感器(打开 关闭)
● 什么是传感器 ● 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 ● iPhone5中内置的传感器有 ● 运动传感器\加速度传感器\加速计(Motion/Acceleromet ...