zoukankan      html  css  js  c++  java
  • Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth

    题目连接:

    http://www.codeforces.com/contest/616/problem/C

    Description

    You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.

    Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.

    For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.

    The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit —- the answer modulo 10. The matrix should be printed without any spaces.

    To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.

    As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

    input

    The first line contains two integers n, m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the field.

    Each of the next n lines contains m symbols: "." for empty cells, "*" for impassable cells.

    Output

    Print the answer as a matrix as described above. See the examples to precise the format of the output.

    Sample Input

    3 3

    .

    .*.

    .

    Sample Output

    3.3

    .5.

    3.3

    Hint

    题意

    给你n行m列的矩阵,矩阵*表示障碍,.表示空地

    对于每一个障碍,让你输出去掉这个障碍之后,这个点所在的连通块的大小是多少

    答案需要%10

    题解:

    这个块的联通块大小,实际上是由四个部分所组成的

    上下左右的连通块加在一起就好了

    但是有一个问题,就是你有可能加重复,所以我们需要有个vis处理

    处理原来连通块,我是用的带权并查集去做的

    dfs和bfs应该都可以,因为复杂度都是差不多的(其实应该比并查集还低

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    char s[1150][1150];
    int fa[1010050];
    int num[1100500];
    int v[1010050];
    int n,m;
    int id(int x,int y)
    {
        return x*m+y;
    }
    int fi(int x)
    {
        return x == fa[x]?x:fa[x]=fi(fa[x]);
    }
    int uni(int x,int y)
    {
        int p = fi(x),q = fi(y);
        if(p != q)
        {
            fa[p]=fa[q];
            num[q]=num[p]+num[q];
            num[p]=0;
        }
    }
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    int main()
    {
    
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]+1);
        for(int i=0;i<=id(n,m)+5;i++)
            num[i]=1,fa[i]=i;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='*')continue;
                for(int k=0;k<4;k++)
                {
                    int x = i + dx[k];
                    int y = j + dy[k];
                    if(x<1||x>n||y<1||y>m)continue;
                    if(s[x][y]=='*')continue;
                    uni(id(x,y),id(i,j));
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='.')
                    printf(".");
                else
                {
                    int ans = 1;
                    for(int k=0;k<4;k++)
                    {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        if(x<1||x>n||y<1||y>m)continue;
                        if(s[x][y]=='*')continue;
                        int idx = fi(id(x,y));
                        if(v[idx]==0)
                        {
                            v[idx]=1;
                            ans+=num[idx];
                        }
                    }
                    printf("%d",ans%10);
                    for(int k=0;k<4;k++)
                    {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        if(x<1||x>n||y<1||y>m)continue;
                        if(s[x][y]=='*')continue;
                        int idx = fi(id(x,y));
                        v[idx]=0;
                    }
                }
            }
            printf("
    ");
        }
    }
  • 相关阅读:
    C#并行编程-并发集合
    使用Lucene.Net实现全文检索
    Lucene.Net 站内搜索
    lucene.net helper类 【结合盘古分词进行搜索的小例子(分页功能)】
    Spring学习之Ioc
    Hibernate向MySQL插入中文数据--乱码解决
    MySql表中key的区别
    Myeclipse中相同变量高亮显示
    Mac下配置PHP+Apache+phpMyAdmin+MySql远程链接
    JQuery实现——黑客帝国代码雨效果
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5127173.html
Copyright © 2011-2022 走看看