uva 1160 X-Plosives (并查集)
来源:互联网 发布:小榕软件实验室 编辑:程序博客网 时间:2024/05/21 06:34
题目大意:给你若干个点与点之间的连线,要求这两个点不属于同一个并查集(因为要保证不形成环) 问从上到下依次加入的时候,有多少条边是不满足要求的.
很显然,这是一道并查集的水题.只要保证相连的两个点不在同一并查集就可以了.
但是这道题的输入有点奇怪,多组数据,并且每组数据以-1结尾.输入的部分,参考了 wall_F的博客.
代码如下:
#include <stdio.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>#include<vector>using namespace std;int fa[111111];int find(int x){ if(x==fa[x])return x; return fa[x]=find(fa[x]);}void init(){ for(int i=0;i<=100009;i++) fa[i]=i;}int main(){ int n,m,i,j,x,y; while(scanf("%d",&n)!=-1) { init(); int ans=0; while(n!=-1) { scanf("%d",&m); x=find(n); y=find(m); if(x!=y) { fa[x]=y; } else ans++; scanf("%d",&n); } printf("%d\n",ans); } return 0;}
0 0
- UVa 1160 X-Plosives (并查集)
- UVa 1160 X-Plosives(并查集)
- uva 1160 X-Plosives (并查集)
- UVA 1160 - X-Plosives(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160X-Plosives【并查集】
- UVA 1160 X-Plosives(并查集)
- X-Plosives - UVa 并查集
- Uva 1160 X-Plosives ——并查集
- uva 1160 - X-Plosives(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160 X-Plosives(并查集)
- uva 1160 X-Plosives(并查集:基础运用)
- UVA 1160 - X-Plosives(并查集)
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
- UVA 1160 - X-Plosives 并查集判断是否存在环
- LA 3644 - X-Plosives,并查集
- LA 3644 X-Plosives / 并查集
- linux文本处理
- TCP C/S 示例详解
- 第五章作业
- 北京地铁的第三轨
- Servlet 各种监听器
- uva 1160 X-Plosives (并查集)
- 深入理解Direct3D9
- Java Sax解析xml
- Qt之模型/视图(实时更新数据)
- 大学教师植“木马”窃取电脑程序获刑
- stubview
- windows2003 安装Visual Studio2008 出错
- 关于使用IO复用和多线程问题
- 【数学 多重集排列】HDU 1261 字串数