Download 2016/finals/maximinimax_flow.cpp from hackercupai/hackercup: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/datasets/hackercupai/hackercup/resolve/main/2016/finals/maximinimax_flow.cpp
- Command line
-
hf download hf://datasets/hackercupai/hackercup/2016/finals/maximinimax_flow.cpp
-
curl -L -o maximinimax_flow.cpp https://huggingface.co/datasets/hackercupai/hackercup/resolve/main/2016/finals/maximinimax_flow.cpp
5.11 kB
| // MAXIMINIMAX FLOW | |
| // Official Solution by Jacob Plachta | |
| using namespace std; | |
| template<typename T> T Abs(T x) { return(x<0 ? -x : x); } | |
| template<typename T> T Sqr(T x) { return(x*x); } | |
| string plural(string s) { return(Sz(s) && s[Sz(s)-1]=='x' ? s+"en" : s+"s"); } | |
| const int INF = (int)1e9; | |
| const LD EPS = 1e-9; | |
| const LD PI = acos(-1.0); | |
| //#if DEBUG | |
| /*#else | |
| #define GETCHAR getchar_unlocked | |
| #endif*/ | |
| bool Read(int &x) | |
| { | |
| char c,r=0,n=0; | |
| x=0; | |
| for(;;) | |
| { | |
| c=GETCHAR(); | |
| if ((c<0) && (!r)) | |
| return(0); | |
| if ((c=='-') && (!r)) | |
| n=1; | |
| else | |
| if ((c>='0') && (c<='9')) | |
| x=x*10+c-'0',r=1; | |
| else | |
| if (r) | |
| break; | |
| } | |
| if (n) | |
| x=-x; | |
| return(1); | |
| } | |
| bool ReadLL(LL &x) | |
| { | |
| char c,r=0,n=0; | |
| x=0; | |
| for(;;) | |
| { | |
| c=GETCHAR(); | |
| if ((c<0) && (!r)) | |
| return(0); | |
| if ((c=='-') && (!r)) | |
| n=1; | |
| else | |
| if ((c>='0') && (c<='9')) | |
| x=x*10+c-'0',r=1; | |
| else | |
| if (r) | |
| break; | |
| } | |
| if (n) | |
| x=-x; | |
| return(1); | |
| } | |
| LL BLT[2][2][LIM2+1]; | |
| void Update(int a,int b,int i,int v) | |
| { | |
| for (; i<=LIM2; i+=(i&-i)) | |
| BLT[a][b][i]+=v; | |
| } | |
| void Update2(int a,int i,int v) | |
| { | |
| Update(a,0,i,v); | |
| Update(a,1,i,i*v); | |
| } | |
| LL Query(int a,int b,LL ii) | |
| { | |
| int i=(int)min(ii,(LL)LIM2); | |
| LL v=0; | |
| for(; i>0; i-=(i&-i)) | |
| v+=BLT[a][b][i]; | |
| return(v); | |
| } | |
| int main() | |
| { | |
| if (DEBUG) | |
| freopen("in.txt","r",stdin); | |
| //vars | |
| int T,t; | |
| int N,M; | |
| int i,j,k; | |
| LL r1,r2,m,m2; | |
| LL tot,rem,cnt,sum; | |
| LL ans; | |
| static vector<PR> con[LIM]; | |
| static int C[LIM],dep[LIM],cyc[LIM]; | |
| static PR par[LIM]; | |
| queue<int> Q; | |
| set<PR> S; | |
| set<PR>::iterator I; | |
| //testcase loop | |
| Read(T); | |
| Fox1(t,T) | |
| { | |
| //input | |
| Read(N),Read(M); | |
| Fox(i,N) | |
| { | |
| Read(j),Read(k),Read(C[i]); | |
| j--,k--; | |
| con[j].pb(mp(k,i)); | |
| con[k].pb(mp(j,i)); | |
| } | |
| //BFS to find the single cycle | |
| Fill(dep,-1); | |
| Q.push(0),dep[0]=0; | |
| for(;;) | |
| { | |
| i=Q.front(),Q.pop(); | |
| Fox(j,Sz(con[i])) | |
| if ((k=con[i][j].x)!=par[i].x) | |
| if (dep[k]>=0) | |
| goto Done; | |
| else | |
| Q.push(k),dep[k]=dep[i]+1,par[k]=mp(i,con[i][j].y); | |
| } | |
| //retrace all edges in the cycle | |
| Done:; | |
| Fill(cyc,0); | |
| cyc[con[i][j].y]=1; | |
| while (i!=k) | |
| if (dep[i]>dep[k]) | |
| { | |
| cyc[par[i].y]=1; | |
| i=par[i].x; | |
| } | |
| else | |
| { | |
| cyc[par[k].y]=1; | |
| k=par[k].x; | |
| } | |
| //init BLTs | |
| Fox(i,N) | |
| { | |
| Update2(cyc[i],C[i],1); | |
| if (cyc[i]) | |
| S.insert(mp(C[i],i)); | |
| } | |
| //handle operations | |
| ans=0; | |
| while (M--) | |
| { | |
| Read(i); | |
| if (i==1) | |
| { | |
| //delete and re-add the adge | |
| Read(i),i--; | |
| Update2(cyc[i],C[i],-1); | |
| if (cyc[i]) | |
| S.erase(mp(C[i],i)); | |
| Read(C[i]); | |
| Update2(cyc[i],C[i],1); | |
| if (cyc[i]) | |
| S.insert(mp(C[i],i)); | |
| } | |
| else | |
| { | |
| //binary search on the answer | |
| ReadLL(tot); | |
| r1=1,r2=(LL)1e13; | |
| while (r2>r1) | |
| { | |
| m=(r1+r2+1)>>1; | |
| rem=tot; | |
| //non-cycle edges | |
| cnt=Query(0,0,m); | |
| sum=Query(0,1,m); | |
| rem-=cnt*m - sum; | |
| if (rem<0) | |
| goto Bad; | |
| //cycle edges | |
| I=S.begin(),i=I->x; | |
| I++,j=I->x; | |
| if (m<=(j<<1)) | |
| rem-=max(0LL,m - (i+j)); | |
| else | |
| { | |
| m2=(m+1)>>1; | |
| cnt=Query(1,0,m2); | |
| sum=Query(1,1,m2); | |
| rem-=cnt*m2 - sum; | |
| if (m&1) | |
| rem++; | |
| } | |
| if (rem<0) | |
| { | |
| Bad:; | |
| r2=m-1; | |
| continue; | |
| } | |
| r1=m; | |
| } | |
| ans+=r1; | |
| } | |
| } | |
| //output | |
| printf("Case #%d: ",t); | |
| cout << ans << endl; | |
| //reset | |
| Fox(i,N) | |
| con[i].clear(); | |
| while (!Q.empty()) | |
| Q.pop(); | |
| S.clear(); | |
| Fill(BLT,0); | |
| } | |
| return(0); | |
| } |