• 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;
    }
  • 相关阅读:
    Comet OJ
    LOJ#2719. 「NOI2018」冒泡排序 DP+组合+树状数组
    LuoguP6747 『MdOI R3』Teleport 二进制+贪心
    LuoguP6748 『MdOI R3』Fallen Lord 树形DP+set
    LuoguP5576 [CmdOI2019]口头禅 后缀树+线段树+暴力
    LOJ#3161. 「NOI2019」I 君的探险 整体二分+随机化+二进制分组
    LOJ#2085. 「NOI2016」循环之美 莫比乌斯反演+杜教筛
    LuoguP5327 [ZJOI2019]语言 线段树合并+树链求并
    【考试题
    ELK
  • 原文地址:https://www.cnblogs.com/dealer/p/12373801.html
Copyright © 2020-2023  润新知