洛谷·[POI2005]SKA-Piggy Banks 小猪存钱罐【Tarjan & 并查集

初见安~这里是传送门:洛谷P3420

题目描述3

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.

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

任务:

写一段程序包括:

读入存钱罐的数量以及相应的钥匙的位置,求出能打开所有存钱罐的情况下,需要破坏的存钱罐的最少数量并将其输出。

输入格式:

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.

第一行:包括一个整数N(1<=N<=1000000),这是Byteazar the Dragon拥有的存钱罐的数量。

存钱罐(包括它们对应的钥匙)从1到N编号。

接下来有N行:第i+1行包括一个整数x,表示第i个存钱罐对应的钥匙放置在了第x个存钱罐中。

输出格式:

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.

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

输入样例#1: 

4
2
1
2
4

输出样例#1: 

2

题解:

题意很明显——一个罐子一把钥匙,一个罐子可以装多个钥匙,问你需要砸几个罐子才能得到所有罐子里的内容。说白了就是求环的数量。因为这个问题里的关系我们可以用图来表示——将该罐子向里有的钥匙对应的罐子连有向边,Tarjan求强连通分量就可以了。若不是环,那么必定存在砸一个入度为0的点即可解锁所有与之联通的点。但是强连通分量的话自身成环,你砸了其他所有的罐子都没用,但是这个环内破坏一个即可解锁整个环。

当然,并不是说每个环都是独立的,可能多个强连通分量之间还有连边,那样的话同样只砸开入度为0的即可。所以我们先求一边强连通分量再缩点,找入读为0的强连通分量即可。

下面是Tarjan的缩点思路模板代码——

#include<bits/stdc++.h>
#define maxn 1000005
using namespace std;
int n, m;
int read()
{
    int x = 0, ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    return x;
}
struct edge
{
    int to, nxt;
    edge() {}
    edge(int t, int x) {to = t, nxt = x;}
}e[maxn];

int head[maxn], k = 0;
void add(int u, int v)
{
    e[k] = edge(v, head[u]);
    head[u] = k++;
}

int dfn[maxn], low[maxn], stc[maxn], tot= 0 , top = 0, cnt = 0, col[maxn];
bool vis[maxn];
void tarjan(int u)//强连通分量模板操作
{
    dfn[u] = low[u] = ++tot;
    stc[++top] = u;
    vis[u] = 1;
    register int v;
    for(register int i = head[u]; ~i; i = e[i].nxt)
    {
        v = e[i].to;
        if(!dfn[v]) {tarjan(v); low[u] = min(low[u], low[v]);}
        else if(vis[v]) low[u] = min(low[u], dfn[v]);
    }
    
    if(dfn[u] == low[u])
    {
        cnt++;
        do{
            v = stc[top--];
            vis[v] = 0;
            col[v] = cnt;
        }while(v != u);
    }
}

int in[maxn], ans = 0;
int main()
{
//	freopen("in.txt", "r", stdin);
    memset(head, -1, sizeof head);
    n = read();
    register int v;
    for(int i = 1; i <= n; i++)
        v = read(), add(v, i);
        
    for(int i = 1; i <= n; i++)
        if(!dfn[i]) tarjan(i);
    
    for(int i = 1; i <= n; i++)
    {
        for(int j = head[i]; ~j; j = e[j].nxt)//找各个强连通分量之间的连边
        {
            v = e[j].to;
            if(col[i] != col[v]) in[col[v]]++;//记录入度
        }
    }
    
    for(int i = 1; i <= cnt; i++) if(!in[i]) ans++;
    
    printf("%d\n", ans);
    return 0;
}

但是其实还有一个可以省一半时间的思路——直接并查集求环就可以了。原理是一样的,只是这里的环就真的是绝对的环了,所以直接求是没有问题的。

下面是并查集做法——

#include<bits/stdc++.h>
#define maxn 1000005
using namespace std;
int n;
int read()
{
    int x = 0, ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
    return x;
}

int fa[maxn];
int get(int x) {return fa[x] == x? x : fa[x] = get(fa[x]);}

int main()
{
    n = read();
    register int x, ans = 0;
    for(int i = 1; i <= n; i++) fa[i] = i;
    for(int i = 1; i <= n; i++)
    {
        x = read();
        if(get(x) == i) fa[i] = i, ans++;//回来了,断开,ans++
        else fa[i] = x;//否则继续存关系
    }
    printf("%d\n", ans);
    return 0;
}

迎评:)
——End——

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值