Is to give you n points, these points must be on the same X or Y axis. The minimum number of points can satisfy this condition.
Ah, I didn't expect it. In fact, if X is the same, y can be moved together.
The first question is wa n times because the array is too small...
/*Pro: 0Sol: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;}