CF 并查集(或者图) Ice Skating
2012-08-31 21:22
197 查看
就是给你n个点,这些点必须在相同的x或者y轴上。 求最少加多少个点才能满足这个条件。
哎,没想到。其实,相同x,y能走的就连在一块。
这里必须记上一笔,第一题wa了n次就是因为数组开小了。。。
哎,没想到。其实,相同x,y能走的就连在一块。
这里必须记上一笔,第一题wa了n次就是因为数组开小了。。。
/* Pro: 0 Sol: date: */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> using namespace std; int n,a,b,x[1111],y[1111],p[1111]; int find(int x) {return x == p[x] ? x : p[x] = find(p[x]);} int main(){ scanf("%d",&n); for(int i = 1; i <= n; i ++) scanf("%d%d",&x[i],&y[i]),p[i] = i; int ans=0; for(int i = 1; i <= n; i ++){ for(int j = 1; j <= n; j ++){ if(x[i] == x[j] || y[i] == y[j]){ int px = find(i); int py = find(j); if(px!=py) p[px] = py,ans++; } } } printf("%d\n",n-1-ans); return 0; }
#include <cstdio> int n,x[1111],y[1111],vis[1111],ans; void go(int indx){ vis[indx] = true; // printf("%d\n",indx); for(int i = 1; i<= n; i ++) if( (x[i] == x[indx] || y[i] == y[indx] )&& !vis[i]) go(i); } int main(){ scanf("%d",&n); for(int i = 1; i <= n; i ++){ scanf("%d%d",x + i, y + i); } ans = -1; for(int i = 1; i <= n; i ++){ if(!vis[i]) go(i),ans ++; } printf("%d\n",ans); return 0; }
相关文章推荐
- 并查集Codeforces Round #134 (Div. 1), problem: (A) Ice Skating
- CF218C:Ice Skating(并查集)
- CodeForces 217 A.Ice Skating(并查集)
- CodeForces 217A Ice Skating(并查集思路)
- CF-25D - Roads not only in Berland(并查集或者搜索)
- [DFS] cf 134A Ice Skating
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Codeforces Round #134 (Div. 2) C. Ice Skating
- FJUT 3104 海平面上升 最短路 或者 离线+并查集
- [CF#250 Div.2 D]The Child and Zoo(并查集)
- Codeforces---Ice Skating
- cf 300B 并查集
- HDU Problem 2120 Ice_cream's world I 【并查集检查环】
- HDU 2120 Ice_cream's world I(并查集)
- CodeForces 217A Ice Skating(DFS)
- hdoj2120 Ice_cream's world I【并查集】
- HDOJ Ice_cream's world I 2120【并查集判断成环】
- HDOJ 2120 Ice_cream's world I (并查集)
- FJUT 3104 海平面上升 最短路 或者 离线+并查集
- 1034. Head of a Gang (30) (并查集 或者 DFS)