zoukankan      html  css  js  c++  java
  • A. Ice Skating (联通块)

    题目链接:http://codeforces.com/problemset/problem/217/A

    本题题意:在坐标系上有n个点,给出n个点的坐标,然后只能竖直或者横向移动,问最少需要建立几个中间点才能够从一个点出发到达所有的点

    这道题是联通块的题 ,最后求出联通块块数 - 1 就可以了!

    也可以考虑并查集!

    AC代码:

     1 #include <cstdio>
     2 #include <string>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <string.h>
     6 #include <math.h>
     7 #include <vector>
     8  
     9 using namespace std;
    10  
    11 using namespace std;
    12 int n,x[101],y[101],vis[101];
    13 void dfs(int i)
    14 {
    15     vis[i]=1;
    16     for(int j=1;j<=n;j++)
    17     {
    18         if((x[j]==x[i]||y[j]==y[i])&&!vis[j])
    19         {
    20             dfs(j);
    21         }
    22     }
    23 }
    24 int main()
    25 {
    26     int ans=-1;
    27     scanf("%d",&n);
    28     for(int i=1;i<=n;i++)
    29     {
    30         scanf("%d%d",&x[i],&y[i]);
    31     }
    32     for(int i=1;i<=n;i++)
    33     {
    34         if(!vis[i])
    35         {
    36             dfs(i);
    37             ans++;
    38         }
    39     }
    40     printf("%d
    ",ans);
    41     return 0;
    42 }
    43  
    44  
  • 相关阅读:
    植物园偶遇一直喵
    植物园偶遇一直喵
    美食篇
    美食篇
    端午节路过南站
    端午节路过南站
    黄山云海
    黄山云海
    Android (1)
    树和树算法(1)
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11173645.html
Copyright © 2011-2022 走看看