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。。。尽情来鄙视我吧。。。

  • 相关阅读:
    类风湿性关节炎患者肿瘤坏死因子拮抗剂应用和心血管疾病的风险:系统文献复习
    合用DMARDs对持续抗TNF治疗的类风湿关节炎患者的影响:来自英国风湿病生物制剂注册系统的结果
    依那西普治疗幼年型特发性关节炎患者的疗程和停药原因
    TNF治疗早期类风湿关节炎患者严重感染和恶性肿瘤的风险: 随机对照研究的荟萃分析
    生物制剂对银屑病关节炎中轴表现的疗效:对依那西普治疗的一组患者为期12个月的观察性研究
    中国银屑病患者中银屑病关节炎的患病率和特征
    TNF抑制剂在10例新近起病的难治性反应性关节炎患者中的安全性与疗效
    中信国健临床通讯2011年1月第2期目录
    甲氨蝶呤治疗早期未用过DMARD的类风湿性关节炎的疗效预测:来自SWEFOT试验开放期初期的结果
    TopCoder入门教程 sqybi完善版[转载]
  • 原文地址:https://www.cnblogs.com/markliu/p/2534207.html
Copyright © 2011-2022 走看看