Codeforces Round 134 div 2 C题
来源:互联网 发布:屏幕闪电软件 编辑:程序博客网 时间:2024/05/16 07:41
这道题看似有点像数学的题目,但实际上是并查集的应用,输入的结点从1编号到n,然后初始化并查集set[i]=i,然后双重循环判断任意两点是否是一个集合的,如果两个点有x或者y相等,则两点属于一个集合。最后看有多少个集合,得到的数字减1 就是结果。
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;typedef struct Node{ int x; int y;}node;node a[110];int set[110];bool vis[110];int findSet(int x)//查找元素x所属集合 { if(x==set[x]) return x; else return set[x]=findSet(set[x]);}void unionSet(int x,int y)//x和y合并到一个集合中 { int fx=findSet(x); int fy=findSet(y); set[fy]=fx;}int main(){ int n; scanf("%d",&n); int i,j; for(i=1;i<=110;i++) set[i]=i; for(i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); for(i=1;i<=n-1;i++) { for(j=1;j<=n;j++) { if(i==j) continue; if(a[i].x==a[j].x || a[i].y==a[j].y) {unionSet(i,j);} } } int s=0; for(i=1;i<=n;i++) if(set[i]==i) s++;//统计有多少个集合 printf("%d",s-1); //system("pause"); return 0;}
- Codeforces Round 134 div 2 C题
- Codeforces Round #153 (Div. 2) C题
- Codeforces Round #158 (Div. 2) C题
- Codeforces Round #257 (Div. 2) C题
- Codeforces Round #277.5 (Div. 2)(C题)
- Codeforces Round #404 (Div. 2) C题
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Codeforces (441C) Round #252 (Div. 2) C题
- Codeforces Round #134 (Div. 2)
- Codeforces Round #134 (Div. 2)
- Codeforces Round #134 (Div. 2)
- Codeforces Round #134 (Div. 2)C. Ice Skating
- Codeforces Round #134 (Div. 2) C. Ice Skating
- Codeforces Round #261 (Div. 2)C题(思维题)
- Codeforces Round #290 (Div. 2) B题 C题
- Codeforces Round #215 (Div. 2) A——C题
- Codeforces Round #279 (Div. 2) (C题)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C题
- Codeforces Round #134 (Div. 2)
- LSA/PLSA
- 2. Uboot Makefile详细分析
- 变量转换二
- 非常实用的Ubuntu命令大全
- Codeforces Round 134 div 2 C题
- hdu 1754
- 单链表之约瑟夫环(Josephus)
- UVA 537 人工智能
- dedecms织梦系统不提取第一张图片为缩略图怎么办
- 将水晶报表移植到另一个项目时遇到的问题的解决办法
- 数论中的基础知识
- Android开源项目-Jamendo音乐播放器研究与优化-开篇
- poj 2365 Find a multiple