CF and query set (or figure) Ice skating

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.