library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test link cut
(test/aoj/2313.test.cpp)

Depends on

Code

// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2313

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../maxflow/dinic.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,e,q;
  cin>>n>>e>>q;
  Dinic<int, true> G(n);
  vector< map<int, int> > I(n);
  for(int i=0;i<e;i++){
    int f,t;
    cin>>f>>t;
    f--;t--;
    I[f][t]=G.add_edge(f,t,1);
    I[t][f]=G.add_edge(t,f,1);
  }

  vector<int> ms(q),as(q),bs(q);
  for(int i=0;i<q;i++){
    cin>>ms[i]>>as[i]>>bs[i];
    as[i]--;bs[i]--;
    if(I[as[i]].count(bs[i])) continue;
    I[as[i]][bs[i]]=G.add_edge(as[i],bs[i],0);
    I[bs[i]][as[i]]=G.add_edge(bs[i],as[i],0);
  }

  int res=G.flow(0,n-1);
  for(int i=0;i<q;i++){
    if(ms[i]==1){
      res+=G.link(0,n-1,as[i],I[as[i]][bs[i]],1);
      res+=G.link(0,n-1,bs[i],I[bs[i]][as[i]],1);
    }
    if(ms[i]==2){
      res-=G.cut(0,n-1,as[i],I[as[i]][bs[i]]);
      res-=G.cut(0,n-1,bs[i],I[bs[i]][as[i]]);
    }
    cout<<res<<"\n";
  }
  return 0;
}
#line 1 "test/aoj/2313.test.cpp"
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2313

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "maxflow/dinic.cpp"

#line 3 "maxflow/dinic.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// O(E V^2)
// O(E \min(E^{1/2}, V^{2/3})) if caps are constant
template<typename Flow,bool directed>
struct Dinic{
  struct Edge {
    int dst;
    Flow cap;
    int rev;
    Edge(int dst,Flow cap,int rev):dst(dst),cap(cap),rev(rev){}
  };

  vector< vector<Edge> > G;
  vector<int> level,iter;

  Dinic(int n):G(n),level(n),iter(n){}

  int add_edge(int src,int dst,Flow cap){
    int e=G[src].size();
    int r=(src==dst?e+1:G[dst].size());
    G[src].emplace_back(dst,cap,r);
    G[dst].emplace_back(src,directed?0:cap,e);
    return e;
  }

  void bfs(int s){
    fill(level.begin(),level.end(),-1);
    queue<int> que;
    level[s]=0;
    que.emplace(s);
    while(!que.empty()){
      int v=que.front();que.pop();
      for(Edge &e : G[v]) {
        if(e.cap>0 and level[e.dst]<0){
          level[e.dst]=level[v]+1;
          que.emplace(e.dst);
        }
      }
    }
  }

  Flow dfs(int v,int t,Flow f){
    if(v==t) return f;
    for(int &i=iter[v];i<(int)G[v].size();i++){
      Edge &e=G[v][i];
      if(e.cap>0 and level[v]<level[e.dst]){
        Flow d=dfs(e.dst,t,min(f,e.cap));
        if(d==0) continue;
        e.cap-=d;
        G[e.dst][e.rev].cap+=d;
        return d;
      }
    }
    return 0;
  }

  Flow flow(int s,int t,Flow lim){
    Flow fl=0;
    while(1){
      bfs(s);
      if(level[t]<0 or lim==0) break;
      fill(iter.begin(),iter.end(),0);

      while(1){
        Flow f=dfs(s,t,lim);
        if(f==0) break;
        fl+=f;
        lim-=f;
      }
    }
    return fl;
  }

  Flow flow(int s,int t){
    return flow(s,t,numeric_limits<Flow>::max()/2);
  }

  Flow cut(int s,int t,int x,int a){
    static_assert(directed, "must be directed");
    auto &e=G[x][a];
    int y=e.dst;
    Flow cr=G[y][e.rev].cap;
    if(cr==0) return e.cap=0;
    e.cap=G[y][e.rev].cap=0;
    Flow cap=cr-flow(x,y,cr);
    if(x!=s and cap!=0) flow(x,s,cap);
    if(t!=y and cap!=0) flow(t,y,cap);
    return cap;
  }

  Flow link(int s,int t,int x,int a,Flow f){
    auto &e=G[x][a];
    e.cap+=f;
    return flow(s,t,f);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 8 "test/aoj/2313.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,e,q;
  cin>>n>>e>>q;
  Dinic<int, true> G(n);
  vector< map<int, int> > I(n);
  for(int i=0;i<e;i++){
    int f,t;
    cin>>f>>t;
    f--;t--;
    I[f][t]=G.add_edge(f,t,1);
    I[t][f]=G.add_edge(t,f,1);
  }

  vector<int> ms(q),as(q),bs(q);
  for(int i=0;i<q;i++){
    cin>>ms[i]>>as[i]>>bs[i];
    as[i]--;bs[i]--;
    if(I[as[i]].count(bs[i])) continue;
    I[as[i]][bs[i]]=G.add_edge(as[i],bs[i],0);
    I[bs[i]][as[i]]=G.add_edge(bs[i],as[i],0);
  }

  int res=G.flow(0,n-1);
  for(int i=0;i<q;i++){
    if(ms[i]==1){
      res+=G.link(0,n-1,as[i],I[as[i]][bs[i]],1);
      res+=G.link(0,n-1,bs[i],I[bs[i]][as[i]],1);
    }
    if(ms[i]==2){
      res-=G.cut(0,n-1,as[i],I[as[i]][bs[i]]);
      res-=G.cut(0,n-1,bs[i],I[bs[i]][as[i]]);
    }
    cout<<res<<"\n";
  }
  return 0;
}
Back to top page