题目描述

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 NNlines: 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
思路:建图+tarjin缩点。最后入度为0的点的个数就是所要求的答案。
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
using namespace std;
map<int,int>ma[MAXN];
int col[MAXN];
int n,tot,tim,ans,top,sumcol;
int vis[MAXN],dfn[MAXN],low[MAXN];
int to[MAXN],net[MAXN],head[MAXN];
int stack[MAXN],visstack[MAXN],into[MAXN];
void add(int u,int v){
to[++tot]=v;net[tot]=head[u];head[u]=tot;
}
void tarjin(int now){
low[now]=dfn[now]=++tim;
stack[++top]=now;
vis[now]=;
visstack[now]=;
for(int i=head[now];i;i=net[i])
if(visstack[to[i]])
low[now]=min(low[now],dfn[to[i]]);
else if(!vis[to[i]]){
tarjin(to[i]);
low[now]=min(low[now],low[to[i]]);
}
if(low[now]==dfn[now]){
sumcol++;
col[now]=sumcol;
while(stack[top]!=now){
col[stack[top]]=sumcol;
visstack[stack[top]]=;
top--;
}
visstack[now]=;
top--;
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
add(x,i);
}
for(int i=;i<=n;i++)
if(!vis[i]) tarjin(i);
for(int i=;i<=n;i++)
for(int j=head[i];j;j=net[j])
if(col[i]!=col[to[j]])
if(ma[col[i]].find(col[to[j]])==ma[col[i]].end()){
ma[col[i]][col[to[j]]]=;
into[col[to[j]]]++;
}
for(int i=;i<=sumcol;i++)
if(into[i]==) ans++;
cout<<ans;
}

这个题也可以用并查集做。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000001
using namespace std;
int n,ans;
int fa[MAXN];
int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
int dx=find(x);
int dy=find(i);
fa[dy]=dx;
}
for(int i=;i<=n;i++)
if(fa[i]==i) ans++;
cout<<ans;
}
 

洛谷 P3420 [POI2005]SKA-Piggy Banks的更多相关文章

  1. [BZOJ1529][POI2005]ska Piggy banks

    [BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...

  2. BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )

    每一连通块砸开一个就可以拿到所有的钱, 所以用并查集求连通块数 ------------------------------------------------------------------- ...

  3. 【BZOJ1529】[POI2005]ska Piggy banks Tarjan

    [BZOJ1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...

  4. bzoj1529 [POI2005]ska Piggy banks 并查集

    [POI2005]ska Piggy banks Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1508  Solved: 763[Submit][Sta ...

  5. Taran 缩点【bzoj1529】[POI2005]ska Piggy banks

    [bzoj1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...

  6. 洛谷 P3420 [POI2005]SKA-Piggy Banks 题解

    蒟蒻的第二篇题解 嗯,直接进入正题 先告诉你们这是并查集,好吧,标签上面有,再来分析这为什么是并查集. 根据题意: 每一个存钱罐能够用相应的钥匙打开或者被砸开,Byteazar已经将钥匙放入到一些存钱 ...

  7. 【BZOJ】1529 [POI2005]ska Piggy banks

    [算法](强连通分量)并查集 [题解] 1.用tarjan计算强连通分量并缩点,在新图中找入度为0的点的个数就是答案. 但是,会爆内存(题目内存限制64MB). 2.用并查集,最后从1到n统计fa[i ...

  8. BZOJ 1529 [POI2005]ska Piggy banks(并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1529 [题目大意] 给出一张n个点n条边的有向图,问选取几个点为起点可以遍历全图 [题 ...

  9. BZOJ 1529 [POI2005]ska Piggy banks:并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1529 题意: Byteazar有N个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. By ...

随机推荐

  1. SQL-Oracle存储过程-循环A表,向B表插入数据

    --存储过程,查询A表,向B表插入数据 create or replace procedure prc_sg_sjtj_config(p_flag out varchar2) IS BEGIN FOR ...

  2. Linux线程资源限制

  3. python的range()函数使用方法

    python的range()函数使用非常方便.它能返回一系列连续添加的整数,它的工作方式类似于分片.能够生成一个列表对象. range函数大多数时常出如今for循环中.在for循环中可做为索引使用.事 ...

  4. 在 Eclipse 中使用 C++

    安装 安装Eclipse Eclipse下载页 能够选择Eclipse IDE for C/C++ Developers(内置CDT插件) 也能够选择安装其它版本号之后再安装CDT插件. 安装CDT插 ...

  5. poj3685(嵌套二分)

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 4658   Accepted: 1189 Descriptio ...

  6. Gretna2.0 使用过程中遇到的问题

    在做Normalize的时候,报错"Cannot find T1 image (e.g. *.dcm in T1 Directory), Please Check again!", ...

  7. 英语发音规则---V字母

    英语发音规则---V字母 一.总结 一句话总结: 1.V发[v]? voice [vɒɪs] n. 声音 love [lʌv] n. 恋爱 leave [liːv] vt. 离开 very ['ver ...

  8. poj--1383--Labyrinth(树的直径)

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4062   Accepted: 1529 Descrip ...

  9. TP5异常处理

    TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...

  10. 40.DOM读取XML

    main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...