1529: [POI2005]ska Piggy banks
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 817 Solved: 366
[ Submit][ Status]
Description
Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取出来. 他想尽量少的打破存钱罐取出所有的钱,问最少要打破多少个存钱罐.
Input
第一行一个整数 N (1 <= N <= 1.000.000) – 表示存钱罐的总数. 接下来每行一个整数,第 i+1行的整数代表第i个存钱罐的钥匙放置的存钱罐编号.
Output
一个整数表示最少打破多少个存钱罐.
Sample Input
4
2
1
2
4
2
1
2
4
Sample Output
2
In the foregoing example piggy banks 1 and 4 have to be smashed.
终于突破 rank 1000 了哈哈哈哈哈
这道题很容易让人想成SCC,这其实也是对的,只不过,据hzwer所说,要MLE
这道题,首先所有点的出度为1,和1124那道题很像
图中一定是一条条链指向一个环,那么用并查集即可
(怎么感觉直接统计入度为0的点+随便乱搞捏?等noip完了再试试。。。)
//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive
************************************************/
/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
/
int n;
int fa[1000010];
/
inline int getint()
{
int res=0;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))res=res*10+c-'0',c=getchar();
return res;
}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
/
void solve()
{
///init///
int fu,fv;
int ans;
calculate
ans=n=getint();
rep(i,1,n)fa[i]=i;
rep(i,1,n)
{
fu=find(i),fv=find(getint());
if(fu!=fv)ans--,fa[fu]=fv;
}
/output/
printf("%d\n",ans);
}
/
int main()
{
#ifndef _TEST
freopen("std.in","r",stdin); freopen("std.out","w",stdout);
#endif
solve();
#ifdef _TEST
for(;;);
#endif
return 0;
}