bzoj1529: [POI2005]ska Piggy banks
2016-06-12 17:17
399 查看
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1529
题目大意:Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取出来. 他想尽量少的打破存钱罐取出 所有的钱,问最少要打破多少个存钱罐.
题解:并查集
代码:
View Code
题目大意:Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取出来. 他想尽量少的打破存钱罐取出 所有的钱,问最少要打破多少个存钱罐.
题解:并查集
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define maxn 1000005 using namespace std; int n,tot,ans; int fa[maxn]; int read() { int x=0; char ch; bool bo=0; while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1; while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9'); if (bo) return -x; return x; } int find(int x) { if (fa[x]!=x) fa[x]=find(fa[x]); return fa[x]; } int main() { n=read(); for (int i=1; i<=n; i++) fa[i]=i; for (int i=1; i<=n; i++) { int x=read(); int q=find(i),p=find(x); if (q!=p) fa[q]=p; } for (int i=1; i<=n; i++) if (fa[i]==i) ans++; printf("%d\n",ans); }
View Code
相关文章推荐
- C#编写ActiveX网页截图控件
- Android属性动画
- Appium+Robotframework实现Android应用的自动化测试-6:一个简单的例子
- 分页
- Android下模拟点击事件
- megacli 开启强制写入硬盘
- VUEJS 实战教程第二章,修复错误并且美化时间
- image hover
- (转)Tomcat调优
- 全自动,企业级,springmvc,
- android Service 的简单使用
- OpenGL 画出五角星
- MySQL存储引擎的比较
- sched clock
- MIPS指令集
- RabbitMQ安装与使用
- 非关系型的数据库NoSQL
- JAVA 中线程队列BlockingQueue的使用
- libuv学习笔记(7)
- Ext Js MVC系列一 环境搭建和MVC框架整体认识