Advertisement
Akatsuki13

Articulation Zu vai

Oct 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. int bothCapacity[MAXN][MAXN];
  2. vector<int>bothEdges[MAXN];
  3.  
  4. int BFSforMaxFlow(int source,int destination){ //Directed graph. No cycle
  5.     queue<int>Q;
  6.     int i,parent[MAXN],top,next,len,answer;
  7.     bool visited[MAXN];
  8.     Q.push(source);
  9.     for(i=0;i<MAXN;i++){
  10.         visited[i]=false;
  11.         parent[i]=-1;
  12.     }
  13.     visited[source]=true;
  14.     while(!Q.empty()){
  15.         top=Q.front();
  16.         Q.pop();
  17.         len=bothEdges[top].size();
  18.         for(i=0;i<len;i++){
  19.             next=bothEdges[top][i];
  20.             if(visited[next]||bothCapacity[top][next]==0)
  21.                 continue;
  22.             Q.push(next);
  23.             visited[next]=true;
  24.             parent[next]=top;
  25.             if(next==destination){
  26.                 while(!Q.empty())
  27.                     Q.pop();
  28.                 break;
  29.             }
  30.         }
  31.     }
  32.     top=destination;answer=INF;
  33.     while(parent[top]!=-1){
  34.         next=parent[top];
  35.         answer=min(answer,bothCapacity[next][top]);
  36.         top=next;
  37.     }
  38.     top=destination;
  39.     while(parent[top]!=-1){
  40.         next=parent[top];
  41.         bothCapacity[next][top]-=answer;
  42.         bothCapacity[top][next]+=answer;
  43.         top=next;
  44.     }
  45.     if(answer==INF)
  46.         return 0;
  47.     return answer;
  48. }
  49.  
  50. int MaxFlow(int source,int destination,int capacity[MAXN][MAXN],vector<int>edges[MAXN]){ //Directed graph. No cycle
  51.     int answer=0,i,j,len,next,temp;
  52.     for(i=0;i<MAXN;i++)
  53.         for(j=0;j<MAXN;j++)
  54.             bothCapacity[i][j]=0;
  55.     for(i=0;i<MAXN;i++){
  56.         len=edges[i].size();
  57.         for(j=0;j<len;j++){
  58.             next=edges[i][j];
  59.             bothEdges[i].push_back(next);
  60.             bothEdges[next].push_back(i);
  61.             bothCapacity[next][i]=0;
  62.             bothCapacity[i][next]=capacity[i][next];
  63.         }
  64.     }
  65.     while(true){
  66.         temp=BFSforMaxFlow(source,destination);
  67.         if(temp==0)
  68.             break;
  69.         answer+=temp;
  70.     }
  71.     return answer;
  72. }
  73.  
  74. void articulationPointAndBridge (int u) {
  75.     bool thisArticulationPoint=false;
  76.     dfs_low[u]=dfs_num[u]=predfn++; //dfs_low[u]<=dfs_num
  77.     for(int j=0;j<edge[u].size();j++) {
  78.         int v=edge[u][j];
  79.         if(dfs_num[v]==UNVISITED) {
  80.             dfs_parent[v]=u;
  81.             if(u==dfsRoot) rootChild++; //Remember to check in main function
  82.             articulationPointAndBridge(v);
  83.  
  84.             /**For articulation point**/
  85.             if(dfs_low[v]>=dfs_num[u])
  86.                 thisArticulationPoint=true;
  87.             /**For articulation bridge**/
  88.             if(dfs_low[v]>dfs_num[u])
  89.                 bridges.push_back(Edge(u,v));
  90.  
  91.             dfs_low[u]=min(dfs_low[u],dfs_low[v]);
  92.         }
  93.         else if(v!=dfs_parent[u])
  94.             dfs_low[u]=min(dfs_low[u],dfs_low[v]);
  95.     }
  96.     if(thisArticulationPoint)
  97.         articulationPoints.push_back(u);
  98. }
  99.  
  100. void tarjanSCC (int u) {
  101.     dfs_low[u]=dfs_num[u]=predfn++; //dfs_low[u]<=dfs_num
  102.     S.push(u);
  103.     visiting[u]=true;
  104.     for(int j=0;j<edge[u].size();j++) {
  105.         int v=edge[u][j];
  106.         if(dfs_num[v]==UNVISITED)
  107.             tarjanSCC(v);
  108.         if(visiting[v])
  109.             dfs_low[u]=min(dfs_low[u],dfs_low[v]);
  110.     }
  111.     if(dfs_low[u]==dfs_num[u]) {
  112.         while(true) {
  113.             int v=S.top();
  114.             S.pop();
  115.             visiting[v]=false;
  116.             scc[v]=numSCC;
  117.             if(v==u) break;
  118.         }
  119.         numSCC++;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement