zoukankan      html  css  js  c++  java
  • Codeforces Round #122 (Div. 1)>TLE代码 跪求(n^2)的最小割顶集算法(StoerWagner)

    A. Cutting Figure

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected.

    A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition.

    Input

    The first input line contains two space-separated integers n and m (1 ≤ n, m ≤ 50) — the sizes of the sheet of paper.

    Each of the next n lines contains m characters — the description of the sheet of paper: the j-th character of the i-th line equals either "#", if the corresponding square is painted (belongs to set A), or equals "." if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty.

    Output

    On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1.

    Sample test(s)
    Input
    5 4 
    ####
    #..#
    #..#
    #..#
    ####
    Output
    2
    Input
    5 5 
    #####
    #...#
    #####
    #...#
    #####
    Output
    2
    Note

    In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.

    The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses.

    View Code
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    #define V 2550
    #define E 300000
    #define inf 0xffff
    int map[V][2];
    int flag[V][V];
    char g[55][55];
    int log[55][55];
    struct Edge
    {
        int u,v,c,next;
    }edge[E];
    int n,m,cnt;
    int dist[V];
    int head[V];
    int que[V];
    int sta[V];
    int s,t;
    void init(){
        cnt=0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int c){
        edge[cnt].u=u;edge[cnt].v=v;edge[cnt].c=c;
        edge[cnt].next=head[u];head[u]=cnt++;
        edge[cnt].u=v;edge[cnt].v=u;edge[cnt].c=0;
        edge[cnt].next=head[v];head[v]=cnt++;
    }
    
    int dinic(int s,int t){
        int ans=0;
        while(true){
            int left,right,u,v;
            memset(dist,-1,sizeof(dist));
            left=right=0;
            que[right++]=s;
            dist[s]=0;
    
            while(left<right){
                u=que[left++];
                for(int k=head[u];k!=-1;k=edge[k].next){
                    u=edge[k].u;
                    v=edge[k].v;
                    if(edge[k].c > 0 && dist[v]==-1){
                        dist[v]=dist[u]+1;
                        que[right++]=v;
                        if(v==t){
                            left=right;
                            break;
                        }
                    }
                }
            }
    
            if(dist[t]==-1) break;
    
            int top=0;
            int now=s;
    
            while(true){
                if(now!=t){
                    int k;
                    for(k=head[now];k!=-1;k=edge[k].next){
                        if(edge[k].c>0 && dist[edge[k].v]==dist[edge[k].u]+1) break;
                    }
                    if(k!=-1){
                        sta[top++]=k;
                        now=edge[k].v;
                    }
                    else{
                        if(top==0) break;
                        dist[edge[sta[--top]].v]=-1;
                        now=edge[sta[top]].u;
                    }
                }
                else{
                    int flow=inf,ebreak;
                    for(int i=0;i<top;i++){
                        if(flow>edge[sta[i]].c){
                            flow=edge[sta[i]].c;
                            ebreak=i;
                        }
                    }
                    ans+=flow;
                    for(int i=0;i<top;i++){
                        edge[sta[i]].c-=flow;
                        edge[sta[i]^1].c+=flow;
                    }
                    now=edge[sta[ebreak]].u;
                    top=ebreak;
                }
            }
        }
        return ans;
    }
    
    void build(int x,int y,int ver,int n,int m){
        init();
        for(int i=1;i<=n;i++){
            addedge(i,i+n,1);
        }
        for(int i=0;i<m;i++){
            addedge(map[i][0]+n,map[i][1],inf);
            addedge(map[i][1]+n,map[i][0],inf);
        }
        addedge(x,x+n,inf);
        addedge(y,y+n,inf);
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=0;i<n;i++){
                scanf("%s",g[i]);
            }
            memset(log,0,sizeof(log));
            int cnt=1;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    if(g[i][j]=='#'){
                        log[i][j]=cnt++;
                    }
                    else log[i][j]=0;
            /*for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    cout <<log[i][j];
                }
                cout <<endl;
            }*/
    
            int ver=(cnt-1)*2+1;
            //cout << ver << endl;
            int k=0,a,b;
            memset(map,0,sizeof(map));
            memset(flag,0,sizeof(flag));
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    if(log[i][j]){
    
                        if(log[i+1][j]){
                            a=map[k][0]=log[i][j];
                            b=map[k][1]=log[i+1][j];
                            flag[a][b]=1;
                            //cout << a <<"--" << b << endl;
    
                            k++;
                        }
                        if(log[i-1][j]){
                            a=map[k][0]=log[i][j];
                            b=map[k][1]=log[i-1][j];
                            flag[a][b]=1;
                            //cout << a <<"--" << b << endl;
                            k++;
                        }
                        if(log[i][j-1]){
                            a=map[k][0]=log[i][j];
                            b=map[k][1]=log[i][j-1];
                            flag[a][b]=1;
                            //cout << a <<"--" << b << endl;
                            k++;
                        }
                        if(log[i][j+1]){
                            a=map[k][0]=log[i][j];
                            b=map[k][1]=log[i][j+1];
                            flag[a][b]=1;
                            //cout << a <<"--" << b << endl;
                            k++;
                        }
    
                    }
    
    
    
            /*for(int i=0;i<m;i++){
                scanf(" (%d,%d)",&a,&b);
                a++,b++;
                map[i][0]=a,map[i][1]=b;
                flag[a][b]=1;
            }*/
            if(k==0){
                if((cnt-1)==1) printf("-1\n");
                else printf("0\n");
                continue;
            }
            //cout << k << endl;
            int pre,ans=inf,sign=0;
            for(int i=1;i<=cnt-1;i++){
                for(int j=i+1;j<=cnt-1;j++){
                    if(!flag[i][j]){
                        sign=1;
                        build(i,j,ver,cnt-1,k);
                        ans=min(ans,dinic(i,j+cnt-1));
                    }
                }
            }
            if(sign){
                printf("%d\n",ans);
            }
            else{
                printf("-1\n");
            }
        }
        return 0;
    }

    正解应该用dfs爆之,涂方便,直接用“网络流求割顶集模板n^4”开敲,果断TLE。。。尽情来鄙视我吧。。。

  • 相关阅读:
    Java实现三人年龄
    从session中获取当前用户的工具类
    全局Session-GlobalSession
    Request获取Session的两种方式
    跨域访问sessionid不一致问题
    获得HttpServletRequest 和HttpSession对象
    eclipse pom.xml 报错org.apache.maven.plugin.war.WarMojo的解决办法
    idea与eclipse项目相互导入的过程
    静态工具类中使用注解注入service实例
    解决静态utils里面注入mapper对象
  • 原文地址:https://www.cnblogs.com/markliu/p/2534207.html
Copyright © 2011-2022 走看看