[POI2005]SKA-Piggy Banks

9 篇文章 0 订阅
6 篇文章 0 订阅

[POI2005]SKA-Piggy Banks

(Luogu P3420)

English Problem

Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its corresponding key or smashed. Byteazar has put the keys in some of the piggy banks - he remembers which key has been placed in which piggy bank. Byteazar intends to buy a car and needs to gain access to all of the piggy banks. However, he wants to destroy as few of them as possible. Help Byteazar to determine how many piggy banks have to be smashed.

TaskWrite a programme which:

reads from the standard input the number of piggy banks and the deployment of their corresponding keys,finds the minimal number of piggy banks to be smashed in order to gain access to all of them,writes the outcome to the standard output.

The first line of the standard input contains a single integer NN (1\le N\le 1\ 000\ 0001≤N≤1 000 000) - this is the number of piggy banks owned by the dragon. The piggy banks (as well as their corresponding keys) are numbered from 11 to NN. Next, there are NN lines: the (i+1)(i+1)’st line contains a single integer - the number of the piggy bank in which the ii’th key has been placed.

The first and only line of the standard output should contain a single integer - the minimal number of piggy banks to be smashed in order to gain access to all of the piggy banks.

中文翻译

(By Stockholm_Sun)

Byteazar the Dragon拥有N个小猪存钱罐。每一个存钱罐能够用相应的钥匙打开或者被砸开。Byteazar已经将钥匙放入到一些存钱罐中。现在已知每个钥匙所在的存钱罐,Byteazar想要买一辆小汽车,而且需要打开所有的存钱罐。然而,他想要破坏尽量少的存钱罐,帮助Byteazar去决策最少要破坏多少存钱罐。

任务:
写一段程序包括:
读入存钱罐的数量以及相应的钥匙的位置,求出能打开所有存钱罐的情况下,需要破坏的存钱罐的最少数量并将其输出。

输入格式:
第一行:包括一个整数N(1<=N<=1000000),这是Byteazar the Dragon拥有的存钱罐的数量。
存钱罐(包括它们对应的钥匙)从1到N编号。
接下来有N行:第i+1行包括一个整数x,表示第i个存钱罐对应的钥匙放置在了第x个存钱罐中。

输出格式:
仅一行:包括一个整数,表示能打开所有存钱罐的情况下,需要破坏的存钱罐的最少数量。

输入输出样例
输入样例#1:
4
2
1
2
4
输出样例#1:
2

思路

做这个题有两个思路,第一个就是并查集,但是我一开始并没有想到这种解法。今天重点讲讲第二种我一开始就想到的一个解法。
思路是这样的:通过题意中一个存钱罐中的钥匙能够打开另一个(或者自己本身)存钱罐的关系,我们不难想出这些存钱罐之间的关系可以用一张图来表示,所以我们发现这是一张有向图。那么图中入度为0的所有节点一定需要被砸。
接下来考虑一种特殊情况,就是如果这张有向图中有环,那么这个环就必须有一个需要被打开,然后环内的所有罐都能被打开,所以我们就用Tarjan算法来缩点,将强连通分量做出来,然后会形成一张新图,对于这张新图,我们只需要计算入度为0的节点个数即可,因为入度不是0的节点就一定可以通过砸开它的前驱,取到钥匙并打开来得到更优的解。
还有一点细节,就是对缩点之后的新图中自环的处理,如果出现了自环,那么我们就不统计这一条边(自环)。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<deque>
#include<cstring>
#define MAXX 1000001
using namespace std;
int i,j,m,n,k; 
int temp,hd[MAXX],col[MAXX],colorn,b[MAXX];
int deep[MAXX],dfn[MAXX],low[MAXX],num;
int sta[MAXX],rd[MAXX],top;
struct data
{
    int v;
    int nxt;
}a[MAXX];

int r()
{
    int ans=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ans*=10;
        ans+=ch-'0';
        ch=getchar();
    }
    return ans*f;
}

void add(int x,int y)
{
    a[++temp].nxt=hd[x];
    a[temp].v=y;
    hd[x]=temp;
}

void tarjan(int x)
{
    int son;
    dfn[x]=low[x]=++num;
    b[x]=1;
    sta[++top]=x;
    for(int p=hd[x];p;p=a[p].nxt)
    {
        son=a[p].v;
        if(!dfn[son])
        {
            tarjan(son);
            low[x]=min(low[x],low[son]);
        }
        else if(b[son])
        {
            low[x]=min(low[x],dfn[son]);
        }
    }
    if(dfn[x]==low[x])
    {
        b[x]=0;
        col[x]=++colorn;
        while(sta[top]!=x)
        {
            col[sta[top]]=colorn;
            b[sta[top--]]=0;
        }
        top--;
    }
}

int main()
{
    n=r();
    int xx;
    for(i=1;i<=n;i++)
    {
        xx=r();
        add(xx,i);
    }

    for(i=1;i<=n;i++)
    if(!col[i])
    tarjan(i);

    for(i=1;i<=n;i++)
    for(int p=hd[i];p;p=a[p].nxt)
    {
        if(col[a[p].v]!=col[i])
        rd[col[a[p].v]]++;
    }
    int ans=0;
    for(i=1;i<=colorn;i++)
    if(!rd[i])
    ans++;
    cout<<ans;
    return 0;
}
/*
5
2
3
3
3
3
*/

这里写图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值