Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=1529
The main idea: Byteazar has N Piggy piggy bank. Each piggy bank can only be opened with a key or smashed open. Byteazar has put the keys to each piggy bank in some piggy banks. Byteazar now wants to buy a car so he has to take all the money out. He wanted to break the piggy bank and take out all the money and ask at least how many piggy banks to break.
The puzzle: and check the set
Code:
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <cmath>6 #defineMAXN 10000057 using namespacestd;8 intN,tot,ans;9 intFA[MAXN];Ten intRead () One { A intx=0;CharChBOOLdo{0; - while(Ch=getchar (),ch<'0'|| Ch>'9')if(ch=='-') bo=1; - while(x=x*Ten+ch-'0', Ch=getchar (), ch>='0'&&ch<='9'); the if(BO)return-X;returnx; - } - intFindintx) - { + if(fa[x]!=x) fa[x]=find (Fa[x]); - returnFa[x]; + } A intMain () at { -n=read (); - for(intI=1; i<=n; i++) fa[i]=i; - for(intI=1; i<=n; i++) - { - intx=read (); in intQ=find (i), p=find (x); - if(q!=p) fa[q]=p; to } + for(intI=1; i<=n; i++)if(fa[i]==i) ans++; -printf"%d\n", ans); the}
View Code
bzoj1529: [Poi2005]ska Piggy Banks