zoukankan      html  css  js  c++  java
  • Ice Skating

    Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

    We assume that Bajtek can only heap up snow drifts at integer coordinates.

    Input

    The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

    Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

    Output

    Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

    Examples
    input
    Copy
    2
    2 1
    1 2
    output
    Copy
    1
    input
    Copy
    2
    2 1
    4 1
    output
    Copy
    0

     遍历每个连通的结点,如果遇到没有连通的结点那么添加一个结点即可将它并入连通图中。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    //#include <xfunctional>
    #define ll long long
    #define mod 998244353
    using namespace std;
    int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
    const long long inf = 0x7f7f7f7f7f7f7f7f;
    const int INT = 0x3f3f3f3f;
    
    int n, ans = 0;
    vector<vector< bool>> vis(1005,vector<bool>(1005,false));
    void dfs(int x,int y)
    {
        vis[x][y] = false;
        for (int i = 0; i < 1001; i++)
        {
            if (vis[i][y])
                dfs(i, y);
        }
        for (int i = 0; i < 1001; i++)
        {
            if (vis[x][i])
                dfs(x, i);
        }
    }
    int main()
    {
        cin >> n;
        while(n--)
        {
            int x, y;
            cin >> x >> y;
            vis[x][y] = true;
        }
        for (int i = 0; i < 1005; i++)
        {
            for (int j = 0; j < 1005; j++)
            {
                if (vis[i][j])
                {
                    ans++;
                    dfs(i, j);
                }    
            }
        }
        cout << ans - 1;
        return 0;
    }
  • 相关阅读:
    技术的那些事儿_2_产品与工艺
    for与foreach再探讨
    技术的那些事儿_3_西方技术管理的精髓
    搭建免费的.Net开发环境
    CDN
    servu 系统服务看门狗,自动脱机补丁,自动启动
    .NET Remoting程序开发入门篇
    网管必知远程终端3389端口合理修改秘藉
    反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
    VPS性能测试第一步:CPU测试
  • 原文地址:https://www.cnblogs.com/dealer/p/12373801.html
Copyright © 2011-2022 走看看