bzoj1529: [Poi2005]ska Piggy Banks

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.