题解

1 条题解

  • 0
    @ 2017-10-05 21:01:29

    //----------------------------------------------AC code----------------------------------------------//

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    #define f(a, b) ((a-1)*m+b)
    
    using namespace std;
    
    const int MAXN = 305;
    int T, n, m, cnt;
    char g[MAXN][MAXN];
    int dx[] = {0, 1};
    int dy[] = {1, 0};
    
    struct Edge{
        int nxt, to;
    }edge[MAXN*MAXN<<3];
    
    int head[MAXN*MAXN], edge_num;
    void add_edge(int from, int to){
        edge[++edge_num].nxt = head[from];
        edge[edge_num].to = to;
        head[from] = edge_num;
    }
    
    bool flag;
    int dfn[MAXN*MAXN], low[MAXN*MAXN], dfsn;
    void tarjan(int u, int f){
        int child = 0;
        dfn[u] = low[u] = ++dfsn;
        for(int i = head[u]; i; i = edge[i].nxt){
            if(!dfn[edge[i].to]){
                child++;
                tarjan(edge[i].to, u);
                low[u] = min(low[u], low[edge[i].to]);
                if(f == 0 && child > 1)
                    flag = 1;
                if(f != 0 && low[edge[i].to] >= dfn[u])
                    flag = 1;
            }
            else
                if(edge[i].to != f)
                    low[u] = min(low[u], dfn[edge[i].to]);
        }
    }
    
    
    int main(){
        freopen("board.in", "r", stdin);
        freopen("board.out", "w", stdout);
        scanf("%d", &T);
        while(T--){
            flag = 0;
            memset(low, 0, sizeof low);
            memset(dfn, 0, sizeof dfn);
            dfsn = 0;
            edge_num = 0;
            memset(head, 0, sizeof head);
            memset(edge, 0, sizeof edge);
            memset(g, 0, sizeof g);
            cnt = 0;
            scanf("%d%d", &n, &m);
            for(int i = 1; i <= n; i++)
                scanf("%s", g[i]+1);
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= m; j++)
                    if(g[i][j] == '#')
                        cnt++;
            if(cnt <= 2){
                puts("-1");
                continue;
            }
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m; j++){
                    if(g[i][j] == '#'){
                        for(int k = 0; k < 2; k++){
                            int tx = i + dx[k];
                            int ty = j + dy[k];
                            if(1 <= tx && tx <= n && 1 <= ty && ty <= m && g[tx][ty] == '#'){
                                add_edge(f(i, j), f(tx, ty));
                                add_edge(f(tx, ty), f(i, j));
                            }
                        }
                    }
                }
            }
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= m; j++)
                    if(g[i][j] == '#' && !dfn[f(i, j)])
                        tarjan(f(i, j), 0);
            if(flag)
                puts("1");
            else
                puts("2");
        }
        return 0;
    }
    
  • 1

信息

难度
9
分类
割点割边图结构 点击显示
标签
递交数
6
已通过
4
通过率
67%
上传者