#1969656

Solution for 2594: Reverse a Road II by btk

Source Code Status Test Cases
    Policy: public     Reviewed: 73    
00.27 sec    7324 KB    114 lines     2864 bytes    2016-08-19 22:37
#include<bits/stdc++.h>

using namespace std;

struct I{I(){ios::sync_with_stdio(false);cin.tie(0);}}init;

struct edge{
    int flow,to,rev;
};

typedef vector<edge> E;
typedef vector<E> Graph;
typedef vector<int> VI;
typedef vector<VI> VV;
void addedge(Graph &g,int s,int t,int f,int rf){
    int n=g[s].size();
    int m=g[t].size();
    g[s].push_back(edge{f,t,m});
    g[t].push_back(edge{rf,s,n});
}
VI bfs(Graph &G,int s){
    int N=G.size();
    queue<int> que;
    VI dist(N,-1);
    dist[s]=0;
    que.push(s);
    for(;!que.empty();que.pop()){
        auto v=que.front();
        for(auto &e:G[v])
            if(e.flow>0&&dist[e.to]==-1){
                dist[e.to]=dist[v]+1;
                que.push(e.to);
            }
    }
    return dist;
}

int maxflow(Graph& G,int s,int t){
    int res=0;
    int N=G.size();
    while(true){
        auto dist=bfs(G,s);
        if(dist[t]<0)break;
        vector<size_t> iter(N,0);
        std::function<int(int,int)> dfs=[&](int v,int f){
            if(v==s)return f;
            for(auto &i=iter[v];i<G[v].size();i++){
                edge &e=G[v][i];
                edge &re=G[e.to][e.rev];
                if(re.flow>0&&dist[v]>dist[e.to]){
                    int d=dfs(e.to,min(f,re.flow));
                    if(d>0){e.flow+=d;re.flow-=d;return d;}
                }
            }
            return 0;
        };
        int f;
        while((f=dfs(t,114514))>0)res+=f;
      
    }
    return res;
}

int main(){
    for(int N,M,S,T;cin>>N>>M>>S>>T,N+M+S+T;){
        S--;T--;
        VV edge(N,VI(N,0));
        Graph G(N);
        for(int i=0;i<M;i++){
            int a,b;cin>>a>>b;
            edge[a-1][b-1]++;
        }
        for(int a=0;a<N;a++)
            for(int b=a+1;b<N;b++)
                if(edge[a][b]>0||edge[b][a]>0)
                    addedge(G,a,b,edge[a][b],edge[b][a]);
        int flow=maxflow(G,S,T);
        int add=0;
        int cnt=0;
        queue<int> que;
        VI color(N,-1);
        que.push(S);
        color[S]=0;
        while(que.size()){
            int v=que.front();que.pop();
            for(auto &e:G[v]){
                if(e.flow>0&&color[e.to]==-1){
                    color[e.to]=color[v];
                    que.push(e.to);
                }
            }
        }
        que.push(T);
        color[T]=1;
        while(que.size()){
            int v=que.front();que.pop();
            for(auto &e:G[v]){
                auto &re=G[e.to][e.rev];
                if(re.flow>0&&color[e.to]==-1){
                    color[e.to]=color[v];
                    que.push(e.to);
                }
            }
        }
        for(int a=0;a<N;a++)
            for(int b=0;b<N;b++)
                if(color[a]==1&&color[b]==0&&edge[a][b]>0)
                    cnt+=edge[a][b],add=1;
        cout<<flow+add<<" "<<cnt<<endl;
    }
    return 0;
}


Compile Error Logs:
You are not authorized to see the message.

Status
Judge: 1/1 C++14 CPU: 00.27 sec Memory: 7324 KB Length: 2864 B 2016-08-19 22:37 2016-08-19 22:37
Results for testcases
Case # Verdict CPU Time Memory In Out Case Name
Case #1: : Accepted 00.27 sec 7324 KB
< prev | / | next >  
 
Judge Input #  ( | ) Judge Output #  ( | )


Comments
 
 Under Construction.
 
Categories
 
 
Free Tags