【刷题】BZOJ 1124 [POI2008]枪战Maf

Description

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

Input

输入n人数<1000000 每个人的aim

Output

你要求最后死亡数目的最小和最大可能

Sample Input

8
2 3 2 2 6 7 8 5

Sample Output

3 5

Solution

贪心题
最大值:

  • 一个自环,贡献 \(\text{1}\)
  • 一个大于 \(\text{1}\) 的环,贡献 \(\text{size} - \text{1}\)
  • 一个基环树,贡献 \(\text{size}-\text{leaf}\)

最小值:

  • 入度为 \(\text{0}\) 的点,一定存活,将它们加入队列。
  • 从队列里取出节点,开枪,可能出现新的 \(\text{0}\) 入度的节点。
  • 最后剩下若干个环,每个环的贡献为 \(\text{ceil}(\text{size}/2)\)
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=1000000+10;
int n,size,leaf,ansmin,ansmax,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],shoot[MAXN],vis[MAXN],in[MAXN],out[MAXN],loop,died[MAXN];
std::queue<int> q;
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y)
{
    to[++e]=y;
    nex[e]=beg[x];
    beg[x]=e;
}
inline void dfs1(int x)
{
    vis[x]=1;size++;
    if(in[x]!=1||out[x]!=1)loop=0;
    for(register int i=beg[x];i;i=nex[i])
        if(!vis[to[i]])dfs1(to[i]);
    if(!in[x])leaf++;
}
inline void solve_max()
{
    for(register int i=1;i<=n;++i)
        if(!vis[i])
        {
            loop=1;size=leaf=0;
            dfs1(i);
            if(loop)ansmax+=size-(size==1?0:1);
            else ansmax+=size-leaf;
        }
}
inline void dfs2(int x)
{
    vis[x]=1;size++;
    for(register int i=beg[x];i;i=nex[i])
        if(!vis[to[i]])dfs2(to[i]);
}
inline void solve_min()
{
    memset(vis,0,sizeof(vis));
    for(register int i=1;i<=n;++i)
        if(!in[i])q.push(i);
    while(!q.empty())
    {
        int x=q.front(),nxt;
        q.pop();
        vis[x]=1;
        if(!died[shoot[x]])
        {
            ansmin++;
            died[shoot[x]]=vis[shoot[x]]=1;
            in[nxt=shoot[shoot[x]]]--;
            if(!died[nxt]&&!in[nxt])q.push(nxt);
        }
    }
    for(register int i=1;i<=n;++i)
        if(!vis[i])
        {
            size=0;dfs2(i);
            ansmin+=(size+1.0)/2.0;
        }
}
int main()
{
    read(n);
    for(register int i=1;i<=n;++i)
    {
        read(shoot[i]);
        insert(i,shoot[i]);insert(shoot[i],i);
        in[shoot[i]]++,out[i]++;
    }
    solve_max();solve_min();
    printf("%d %d\n",ansmin,ansmax);
    return 0;
}

转载于:https://www.cnblogs.com/hongyj/p/9516749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值