uvalive 3644 X-Plosives
2015-09-15 19:42
288 查看
思路:简单的并查集应用,两个点有相同的祖先是组成环,会引起爆炸,不能放,num++。
#include <iostream> #include <cstring> #include <stdio.h> using namespace std; int f[100008]; int find(int x) { return x==f[x]?x:f[x]=find(f[x]); } int init() { for(int i=0;i<100008;i++) f[i]=i; return 1; } int main() { //freopen("in.txt","r",stdin); int x,y,num=0; init(); while(scanf("%d",&x)!=EOF) { if(x==-1) { printf("%d\n",num); num=0; init(); continue; } scanf("%d",&y); int xx=find(x); int yy=find(y); if(xx==yy) num++; else f[xx]=yy; find(x); } }
相关文章推荐
- android之音频的播放和录制(MediaPlayer,SoundPool,MediaRecorder)
- 正则表达式
- 破解限制单机上网方法
- UE4 Material - How To Use Fresnel in your Materials
- Android之Fragment(碎片)
- [死锁] dispatch_sync 之于 当前线程
- 对称加密和非对称加密的比较
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
- 规定C++输出每行不超过多少字数
- Error:(16, 0) Gradle DSL method not found: 'runProguard()'
- C#调用C回调函数后,程序奔溃问题
- ThreadLocal存储原理
- UI课程07 UIScrollView、UIPageControl
- Form Post数据 + Iframe跨域回调 全公开
- android数据库持久化框架
- iOS项目开发实战——使用手势识别判断用户操作
- 安卓中实现Activity向Fragment传值
- ASCII码表
- 4K手机能拯救索尼手机吗?
- uva 1584 Circular Sequence (字符串处理)