Bzoj 2095: [Poi2010]bridges (dichotomy + Euler circuit for mixed graphs)

Source: Internet
Author: User
Tags in degrees

Test instructions

Given the N-point M-side of the graph, for the edge u,v, from the U to v Edge is C, from V to u Benquan is D, asked to be able to pass through each edge once and only once the minimum weight and.

Ideas

Two-point answer mid, then cut the weight value is greater than the edge of the mid, the original image becomes a mixed graph with both a non-forward edge and a forward edge, then the problem is converted to find out whether there is a Euler loop on the mixed graph.

undirected graphs exist in the Euler loop, when and only if all vertices of the graph are of an even number and the graph is connected.

The undirected graph exists in the Euler loop, when and only if all vertex degrees in the graph are equal to perceive and the graph is connected.

An edge only passes once, so the final ownership of the non-directed side is the forward edge, that is, we want to give the direction of the non-directional to the existence of the Euler loop.

The non-directed edge is determined in a direction and then calculated in and out, when the X=abs (in-out) is odd, there is no Euler circuit, because no matter how the orientation is not satisfied with the degree and the degree is equal.

Composition: For casually directed non-directional edge (U,V), add a (v,u,1) edge represents the side can be added one v->u at a time, if the degree of > out, by the source point S Edge (S,I,X/2), if the perceive > in degrees, then the Edge (I,T,X/2), respectively, it should be X/2 times to increase the degree/penetration edge.

Run the maximum flow once and the mid is feasible when the network is full.

Code

1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineTrav (u,i) for (int i=front[u];i;i=e[i].nxt)Ten #definefor (A,B,C) for (int a= (b); a<= (c); a++) One using namespacestd; A  -typedefLong Longll; - Const intN = 2e3+Ten; the Const intINF =1e9; -  - ll Read () { -     CharC=GetChar (); +ll f=1, x=0; -      while(!IsDigit (c)) { +         if(c=='-') f=-1; C=GetChar (); A     } at      while(IsDigit (c)) -x=x*Ten+c-'0', c=GetChar (); -     returnx*F; - } -  - structEdge { in     intU,v,cap,flow; - }; to structDinic { +     intn,m,s,t; -     intD[n],cur[n],vis[n]; thevector<int>G[n]; *Vector<edge>es; $queue<int>Q;Panax Notoginseng     voidInitintN) { -          This->n=N; the es.clear (); +for (I,0, N) g[i].clear (); A     } the     voidAddedge (intUintVintW) { +Es.push_back (Edge) {u,v,w,0}); -Es.push_back (Edge) {v,u,0,0}); $m=es.size (); $G[u].push_back (M-2); -G[v].push_back (M-1); -     } the     intBFs () { -memset (Vis,0,sizeof(Vis));WuyiQ.push (s); d[s]=0; vis[s]=1; the          while(!Q.empty ()) { -             intu=Q.front (); Q.pop (); Wufor (I,0,(int) G[u].size ()-1) { -edge& e=Es[g[u][i]]; About                 intv=e.v; $                 if(!vis[v]&&e.cap>e.flow) { -vis[v]=1; -d[v]=d[u]+1; - Q.push (v); A                 } +             } the         } -         returnVis[t]; $     } the     intDfsintUinta) { the         if(U==t| |! AreturnA; the         intflow=0, F; the          for(int& I=cur[u];i<g[u].size (i++);) { -edge& e=Es[g[u][i]]; in             intv=e.v; the             if(d[v]==d[u]+1&& (F=dfs (V,min (a,e.cap-e.flow)) >0) { thee.flow+=F; Aboutes[g[u][i]^1].flow-=F; theFlow+=f; a-=F; the                 if(!a) Break; the             } +         } -         returnflow; the     }Bayi     intMaxflow (intSintt) { the          This->s=s, This->t=T; the         intflow=0; -          while(BFS ()) { -memset (cur,0,sizeof(cur)); theflow+=DFS (s,inf); the         } the         returnflow; the     } - } DC; the  the intN,m,s,t,u[n],v[n],c[n],d[n],inch[N], out[N]; the 94 intCanintM) the { theMemsetinch,0,sizeof(inch)); theMemset out,0,sizeof( out));98Dc.init (n+2); About     intsum=0, X; -for (I,1, M) {101         if(c[i]<=m) out[U[i]]++,inch[v[i]]++;102         if(d[i]<=m) DC. Addedge (V[i],u[i],1);103     }104for (I,1, N)if(ABS (inch[i]- out[i]) &1)return 0; thefor (I,1, N) {106x=inch[i]- out[i];107Sum+=x>0?x>>1:0;108         if(x>0) DC. Addedge (s,i,x>>1);109         if(x<0) DC. Addedge (I,t, (x) >>1); the     }111     returndc. Maxflow (s,t) = =sum; the }113 intMain () the { theN=read (), m=read (); thes=0, t=n+1;117     intL=inf,r=0;118for (I,1, M) {119U[i]=read (), V[i]=read (), C[i]=read (), d[i]=read (); -         if(c[i]>D[i]) swap (c[i],d[i]), swap (u[i],v[i]);121L=min (L,c[i]), r=Max (r,d[i]);122     }123      while(l<R) {124         intm=l+ (r-l)/2; the         if(Can (M)) R=m;Elsel=m+1;126     }127     if(!can (L)) puts ("NIE");Elseprintf"%d", L); -     return 0;129}

Bzoj 2095: [Poi2010]bridges (dichotomy + Euler circuit for mixed graphs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.