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;
    }
  • 相关阅读:
    dlib库+vs2017详细配置流程
    【网易云课堂】【中科院团队】深度学习:算法到实战——神经网络基础
    【网易云课堂】【中科院团队】深度学习:算法到实战——绪论
    matlab 读取多行txt文本
    LeetCode 228. Summary Ranges【未加入列表】
    LeetCode 438. Find All Anagrams in a String
    c++冷知识
    python项目实战——西游记用字统计
    LeetCode 101. Symmetric Tree
    LeetCode 63. Unique Paths II
  • 原文地址:https://www.cnblogs.com/dealer/p/12373801.html
Copyright © 2011-2022 走看看