poj1182
2015-09-28 20:44
381 查看
并查集,这道题以前做过,但是再看到时,很陌生,只知道是并查集,一开始不想想,感觉自己不会,想看答案,可是坚持着想了一下,就想出来了,不过一直wr在只有一组数据,我却写了个while输入,直到文件结尾,差错了很久
参考代码:
http://blog.csdn.net/gemire/article/details/20566907 http://blog.csdn.net/shahdza/article/details/7779230
(食堂里的日本豆腐很好吃,底下有一层很想的鸡蛋,学校门口的菜煎饼很好吃,小的时候很喜欢吃,但是长大后就很少见,上高中的时候,好像还有一次吃晚饭的时间,逃出去买了个菜煎饼,没来得及吃,就往回赶,怕迟到,结果在教室偷偷吃完
)
2015.10.5:
hahaha
参考代码:
http://blog.csdn.net/gemire/article/details/20566907 http://blog.csdn.net/shahdza/article/details/7779230
(食堂里的日本豆腐很好吃,底下有一层很想的鸡蛋,学校门口的菜煎饼很好吃,小的时候很喜欢吃,但是长大后就很少见,上高中的时候,好像还有一次吃晚饭的时间,逃出去买了个菜煎饼,没来得及吃,就往回赶,怕迟到,结果在教室偷偷吃完
)
2015.10.5:
hahaha
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define N 50010 int fa[3*N]; int get_root(int x){ if(fa[x]==x){ return x; } else{ fa[x]=get_root(fa[x]); return fa[x]; } } int main(){ int n,k; int op,a,b; int ans; /*while(*/scanf("%d%d",&n,&k);/*!=EOF){*/ for(int i=1;i<=3*n;i++){ fa[i]=i; } ans=0; for(int i=0;i<k;i++){ scanf("%d%d%d",&op,&a,&b); if(a>n||a<1||b>n||b<1){ ans++; } else{ int ra=get_root(a); int ran=get_root(a+n); int ra2n=get_root(a+2*n); int rb=get_root(b); int rbn=get_root(b+n); int rb2n=get_root(b+2*n); if(op==1){ if(ran==rb||rbn==ra){ ans++; } else{ if(ra!=rb){ fa[ra]=rb; fa[ran]=rbn; fa[ra2n]=rb2n; } } } else if(op==2){ if(ra==rb||rbn==ra){ ans++; } else{ if(ran!=rb){ fa[ran]=rb; fa[ra]=rb2n; fa[ra2n]=rbn; } } } } } printf("%d\n",ans); /*}*/ return 0; }
相关文章推荐
- zoj1383题
- Spring下 将父类属性转化为Json属性字段
- Joda-Time Date(jdk) String相互转换
- LEETCODE-Intersection of Two Linked Lists
- nn.MSECriterion()
- android:ViewPager动画摘要
- windows 开机启动(为了关闭虚拟机的那么多开机进程)
- Isomorphic Strings
- java中重载与重写的区别
- 新版本vsphere支持最大单个vmdk超过2T,理论上支持最大62T
- matlab plot画多条不同颜色的线
- Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator
- 使用链表实现队列
- 操作系统历史PPT
- raid
- machine learning(13) -- solving the problem of overfitting:regularization
- 轻松精通awk数组企业问题案例
- 为什么 AsyncTask 实例必须在UI线程里创建?
- java8新特性学习
- HDU 1026 Ignatius and the Princess I(优先队列+BFS+打印路径)