图论算法模板
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 namespace kruskal//codevs1078 34 { 35 struct bian 36 { 37 int u,v,w; 38 bool op < (const bian &rhs)const {return w<rhs.w;} 39 }bi[10010]; 40 int fa[111],n,ed; 41 int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} 42 int kruskal() 43 { 44 re(i,1,n)fa[i]=i; 45 sort(bi+1,bi+ed+1); 46 int tot=0,ret=0; 47 re(i,1,ed) 48 { 49 int q=find(bi[i].u),w=find(bi[i].v); 50 if(q!=w)fa[q]=w,tot++,ret+=bi[i].w; 51 if(tot==n-1)break; 52 } 53 return ret; 54 } 55 void solve() 56 { 57 inin(n); 58 re(i,1,n)re(j,1,n) 59 { 60 int x;inin(x); 61 if(j>i&&x)ed++,bi[ed].u=i,bi[ed].v=j,bi[ed].w=x; 62 } 63 printf("%d",kruskal()); 64 } 65 } 66 namespace prim//codevs1078 67 { 68 int head[111],next[20020],zhi[20020],w[20020],dis[20020],ed; 69 void add(int a,int b,int c) 70 { 71 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c; 72 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c; 73 } 74 struct wocao 75 { 76 int id,w; 77 wocao(int id=0,int w=0):id(id),w(w){} 78 bool op < (const wocao &rhs)const {return w>rhs.w;} 79 }; 80 priority_queue<wocao>h; 81 bool bo[111]; 82 int prim() 83 { 84 int sum=0; 85 Clear(dis,127);dis[1]=0; 86 h.push(wocao(1,0)); 87 while(!h.empty()) 88 { 89 int x=h.top().id;h.pop(); 90 if(bo[x])continue; 91 bo[x]=1;sum+=dis[x]; 92 for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]&&dis[zhi[i]]>w[i]) 93 dis[zhi[i]]=w[i],h.push(wocao(zhi[i],dis[zhi[i]])); 94 } 95 return sum; 96 } 97 int n; 98 void solve() 99 { 100 inin(n); 101 re(i,1,n)re(j,1,n) 102 { 103 int x;inin(x); 104 if(j>i&&x)add(i,j,x); 105 } 106 printf("%d",prim()); 107 } 108 } 109 namespace floyd//codevs1077 110 { 111 const int inf=2147483647>>1; 112 int a[111][111],n,m,q,w; 113 void floyd() 114 { 115 re(i,1,n)re(j,1,n)re(k,1,n) 116 a[j][k]=min(a[j][k],a[j][i]+a[i][k]); 117 } 118 void solve() 119 { 120 inin(n); 121 re(i,1,n)re(j,1,n)inin(a[i][j]); 122 floyd(); 123 inin(m); 124 re(i,1,m) 125 { 126 inin(q),inin(w); 127 printf("%d\n",a[q][w]); 128 } 129 } 130 } 131 namespace dijkstra//codevs1077 132 { 133 int head[111],next[20020],zhi[20020],w[20020],dis[111][111],ed; 134 int n,m; 135 void add(int a,int b,int c) 136 { 137 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c; 138 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c; 139 } 140 struct wocao 141 { 142 int id,w; 143 wocao(int id=0,int w=0):id(id),w(w){} 144 bool op < (const wocao &rhs)const {return w>rhs.w;} 145 }; 146 priority_queue<wocao>h; 147 bool bo[111]; 148 void dijkstra(int s) 149 { 150 Clear(dis[s],127);Clear(bo,0); 151 dis[s][s]=0; 152 h.push(wocao(s,0)); 153 while(!h.empty()) 154 { 155 int x=h.top().id;h.pop(); 156 if(bo[x])continue; 157 bo[x]=1; 158 for(int i=head[x];i;i=next[i]) 159 if(!bo[zhi[i]]&&dis[s][zhi[i]]>dis[s][x]+w[i]) 160 { 161 dis[s][zhi[i]]=dis[s][x]+w[i]; 162 h.push(wocao(zhi[i],dis[s][zhi[i]])); 163 } 164 } 165 } 166 void solve() 167 { 168 inin(n); 169 re(i,1,n)re(j,1,n) 170 { 171 int x;inin(x); 172 if(j>i&&x)add(i,j,x); 173 } 174 re(i,1,n)dijkstra(i); 175 inin(m); 176 re(i,1,m) 177 { 178 int a,b;inin(a),inin(b); 179 printf("%d\n",dis[a][b]); 180 } 181 } 182 } 183 namespace spfa//codevs1077 184 { 185 int head[111],next[20020],zhi[20020],w[20020],ed,dis[111][111]; 186 int n,m; 187 void add(int a,int b,int c) 188 { 189 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c; 190 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c; 191 } 192 queue<int>h; 193 bool bo[111]; 194 void spfa(int s) 195 { 196 Clear(dis[s],127);dis[s][s]=0; 197 h.push(s); 198 while(!h.empty()) 199 { 200 int x=h.front();h.pop();bo[x]=0; 201 for(int i=head[x];i;i=next[i]) 202 if(dis[s][zhi[i]]>dis[s][x]+w[i]) 203 { 204 dis[s][zhi[i]]=dis[s][x]+w[i]; 205 if(!bo[zhi[i]])bo[zhi[i]]=1,h.push(zhi[i]); 206 } 207 } 208 } 209 void solve() 210 { 211 inin(n); 212 re(i,1,n)re(j,1,n) 213 { 214 int x;inin(x); 215 if(j>i&&x)add(i,j,x); 216 } 217 re(i,1,n)spfa(i); 218 inin(m); 219 re(i,1,m) 220 { 221 int a,b;inin(a),inin(b); 222 printf("%d\n",dis[a][b]); 223 } 224 } 225 } 226 namespace scc//codevs2822 227 { 228 int head[100010],next[200020],zhi[200020],ed; 229 int dfn[100010],low[100010],sccno[100010],num[100010],tot,sum; 230 int sta[100010],top,n,m; 231 void add(int a,int b) 232 { 233 next[++ed]=head[a],head[a]=ed,zhi[ed]=b; 234 } 235 void tarjan(int x) 236 { 237 dfn[x]=low[x]=++tot; 238 sta[++top]=x; 239 for(int i=head[x];i;i=next[i]) 240 { 241 int v=zhi[i]; 242 if(!dfn[v])tarjan(v),low[x]=min(low[x],low[v]); 243 else if(!sccno[v])low[x]=min(low[x],dfn[v]); 244 } 245 if(low[x]==dfn[x]) 246 { 247 sum++; 248 while(1) 249 { 250 int xx=sta[top--]; 251 sccno[xx]=sum,num[sum]++; 252 if(xx==x)break; 253 } 254 } 255 } 256 void find_scc() 257 { 258 re(i,1,n)if(!dfn[i])tarjan(i); 259 } 260 int chu[100010],ans; 261 void rebuild() 262 { 263 re(i,1,n)for(int j=head[i];j;j=next[j]) 264 if(sccno[i]!=sccno[zhi[j]])chu[sccno[i]]++; 265 int bo=-1; 266 re(i,1,sum)if(num[i]>1) 267 { 268 ans++; 269 if(!chu[i]) 270 { 271 if(!bo)continue; 272 else if(bo==-1)bo=i; 273 else bo=0; 274 } 275 } 276 printf("%d\n",ans); 277 if(bo>0) 278 re(i,1,n)if(sccno[i]==bo)printf("%d ",i);else ; 279 else cout<<-1; 280 } 281 void solve() 282 { 283 inin(n),inin(m); 284 re(i,1,m) 285 { 286 int q,w;inin(q),inin(w); 287 add(q,w); 288 } 289 find_scc(); 290 rebuild(); 291 } 292 } 293 namespace bcc 294 { 295 int dfn[100010],cut[100010],bccno[100010],tot,sum; 296 int head[50050],next[100010],zhi[100010],ed; 297 void add(int a,int b) 298 { 299 next[++ed]=head[a],head[a]=ed,zhi[ed]=b; 300 next[++ed]=head[b],head[b]=ed,zhi[ed]=a; 301 } 302 struct bian 303 { 304 int u,v; 305 bian(int u=0,int v=0):u(u),v(v){} 306 }; 307 vector<int>bcc[100010]; 308 stack<bian>s; 309 int dfs(int u,int fa) 310 { 311 int lowu=dfn[u]=++tot; 312 int ne=0; 313 for(int i=head[u];i;i=next[i]) 314 { 315 int v=zhi[i]; 316 bian e(u,v); 317 if(!dfn[v]) 318 { 319 s.push(e),ne++; 320 int lowv=dfs(v,u); 321 lowu=min(lowu,lowv); 322 if(lowv>=dfn[u]); 323 { 324 cut[u]=1; 325 sum++;bcc[sum].clear(); 326 while(1) 327 { 328 bian x=s.top();s.pop(); 329 if(bccno[x.u]!=sum){bcc[sum].push_back(x.u);bccno[x.u]=sum;} 330 if(bccno[x.v]!=sum){bcc[sum].push_back(x.v);bccno[x.v]=sum;} 331 if(x.u==u&&x.v==v)break; 332 } 333 } 334 } 335 else if(dfn[v]<dfn[u]&&v!=fa) 336 { 337 s.push(e); 338 lowu=min(lowu,dfn[v]); 339 } 340 } 341 if(!fa&&ne==1)cut[u]=0; 342 return lowu; 343 } 344 void solve() 345 { 346 int n,m; 347 inin(n),inin(m); 348 re(i,1,m) 349 { 350 int q,w;inin(q),inin(w); 351 add(q,w); 352 } 353 re(i,1,n)dfs(i,0); 354 printf("%d",sum); 355 } 356 } 357 namespace dinic//codevs1993 358 { 359 const int inf=2147483647; 360 int head[222],next[444],zhi[444],cap[444],ed=1,cur[222]; 361 int n,m; 362 void add(int a,int b,int c) 363 { 364 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,cap[ed]=c; 365 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,cap[ed]=0; 366 } 367 queue<int>h;int dis[222]; 368 bool bfs(int s,int t) 369 { 370 Clear(dis,0); 371 dis[s]=1;h.push(s); 372 while(!h.empty()) 373 { 374 int x=h.front();h.pop(); 375 for(int i=head[x];i;i=next[i]) 376 if(cap[i]&&!dis[zhi[i]]) 377 { 378 dis[zhi[i]]=dis[x]+1; 379 h.push(zhi[i]); 380 } 381 } 382 return dis[t]; 383 } 384 int dfs(int x,int Max,int t) 385 { 386 if(x==t||!Max)return Max; 387 int temp,ret=0; 388 for(int &i=cur[x];i;i=next[i]) 389 if(cap[i]&&dis[zhi[i]]==dis[x]+1) 390 { 391 temp=dfs(zhi[i],min(Max,cap[i]),t); 392 ret+=temp,Max-=temp; 393 cap[i]-=temp,cap[i^1]+=temp; 394 if(!Max)return ret; 395 } 396 if(!ret)dis[x]=-1; 397 return ret; 398 } 399 int dinic(int s,int t) 400 { 401 int ret=0; 402 while(bfs(s,t)) 403 { 404 re(i,1,n)cur[i]=head[i]; 405 ret+=dfs(s,inf,t); 406 } 407 return ret; 408 } 409 void solve() 410 { 411 inin(m),inin(n); 412 re(i,1,m) 413 { 414 int q,w,e; 415 inin(q),inin(w),inin(e); 416 add(q,w,e); 417 } 418 printf("%d",dinic(1,n)); 419 } 420 } 421 namespace mcf//codevs1227 422 { 423 const int inf=2147483647; 424 int head[6060],next[1000020],zhi[1000020],cap[1000020],w[1000020],ed=1; 425 void add(int a,int b,int c,int d) 426 { 427 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,cap[ed]=c,w[ed]=d; 428 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,cap[ed]=0,w[ed]=-d; 429 } 430 int n,k,dis[6060],pre[6060]; 431 queue<int>h;bool bo[6060]; 432 bool spfa(int s,int t) 433 { 434 Clear(dis,-1); 435 dis[s]=0,h.push(s); 436 while(!h.empty()) 437 { 438 int x=h.front();h.pop();bo[x]=0; 439 for(int i=head[x];i;i=next[i]) 440 { 441 if(!cap[i]||dis[zhi[i]]>=dis[x]+w[i])continue; 442 dis[zhi[i]]=dis[x]+w[i]; 443 pre[zhi[i]]=i; 444 if(!bo[zhi[i]])h.push(zhi[i]),bo[zhi[i]]=1; 445 } 446 } 447 return dis[t]!=-1; 448 } 449 int mcf(int s,int t) 450 { 451 int ret=0,Min; 452 while(spfa(s,t)) 453 { 454 Min=inf; 455 for(int i=t;pre[i];i=zhi[pre[i]^1]) 456 Min=min(Min,cap[pre[i]]); 457 for(int i=t;pre[i];i=zhi[pre[i]^1]) 458 cap[pre[i]]-=Min,cap[pre[i]^1]+=Min; 459 ret+=Min*dis[t]; 460 } 461 return ret; 462 } 463 void solve() 464 { 465 inin(n),inin(k);int s=6001,t=6002; 466 re(i,1,n)re(j,1,n) 467 { 468 int x;inin(x); 469 int ss=(i-1)*n+j,tt=ss+n*n; 470 add(ss,tt,1,x); 471 add(ss,tt,k,0); 472 if(i<n)add(tt,ss+n,k,0); 473 if(j<n)add(tt,ss+1,k,0); 474 } 475 add(s,1,k,0); 476 add(n*n*2,t,k,0); 477 printf("%d",mcf(s,t)); 478 } 479 } 480 namespace hungary//codevs1022 481 { 482 int head[10010],next[40040],zhi[40040],pre[10010],ed,bo[10010]; 483 void add(int a,int b) 484 { 485 next[++ed]=head[a],head[a]=ed,zhi[ed]=b; 486 next[++ed]=head[b],head[b]=ed,zhi[ed]=a; 487 } 488 int n,m,k,a[111][111]; 489 bool find(int x,int t) 490 { 491 for(int i=head[x];i;i=next[i])if(bo[zhi[i]]!=t) 492 { 493 bo[zhi[i]]=t; 494 if(!pre[zhi[i]]||find(pre[zhi[i]],t)) 495 { 496 pre[zhi[i]]=x; 497 return 1; 498 } 499 } 500 return 0; 501 } 502 void solve() 503 { 504 inin(n),inin(m),inin(k); 505 re(i,1,k) 506 { 507 int x,y; 508 inin(x),inin(y); 509 a[x][y]=1; 510 } 511 re(i,1,n)re(j,1,m)if(!a[i][j]) 512 { 513 if(i<n&&!a[i+1][j])add((i-1)*n+j,(i*n+j)); 514 if(j<m&&!a[i][j+1])add((i-1)*n+j,(i-1)*n+j+1); 515 } 516 int ans=0; 517 re(i,1,n)re(j,1,m)if(!a[i][j])if(find((i-1)*n+j,(i-1)*n+j))ans++; 518 cout<<ans/2; 519 } 520 } 521 namespace point_divide 522 { 523 int n; 524 int head[20020],next[40040],zhi[40040],ed,si[20020],w[20020],v[40040]; 525 int root,sum,ans,bo[20020]; 526 void add(int a,int b,int c) 527 { 528 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,v[ed]=c; 529 next[++ed]=head[b],head[b]=ed,zhi[ed]=a,v[ed]=c; 530 } 531 int gcd(int a,int b) 532 { 533 int c; 534 while(a%b)c=a%b,a=b,b=c; 535 return b; 536 } 537 void getroot(int x,int fa) 538 { 539 si[x]=1;w[x]=0; 540 for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]&&zhi[i]!=fa) 541 { 542 getroot(zhi[i],x); 543 si[x]+=si[zhi[i]]; 544 w[x]=max(w[x],si[zhi[i]]); 545 } 546 w[x]=max(w[x],sum-si[x]); 547 if(w[x]<w[root])root=x; 548 } 549 int dis[20020],shu[3]; 550 void getans(int x,int fa) 551 { 552 shu[dis[x]]++; 553 for(int i=head[x];i;i=next[i]) 554 if(zhi[i]!=fa&&!bo[zhi[i]]) 555 { 556 dis[zhi[i]]=(dis[x]+v[i])%3; 557 getans(zhi[i],x); 558 } 559 } 560 int js(int x,int temp) 561 { 562 shu[0]=shu[1]=shu[2]=0; 563 dis[x]=temp;getans(x,0); 564 return shu[1]*shu[2]*2+shu[0]*shu[0]; 565 } 566 void solve(int x) 567 { 568 ans+=js(x,0); 569 bo[x]=1; 570 for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]) 571 { 572 ans-=js(zhi[i],v[i]); 573 root=0;sum=si[zhi[i]]; 574 getroot(zhi[i],0); 575 solve(root); 576 } 577 } 578 void solve() 579 { 580 inin(n); 581 re(i,2,n) 582 { 583 int q,w,e; 584 inin(q),inin(w),inin(e); 585 add(q,w,e%3); 586 } 587 sum=w[0]=n; 588 getroot(1,0); 589 solve(root); 590 int wocao=gcd(ans,n*n); 591 printf("%d/%d",ans/wocao,n*n/wocao); 592 } 593 } 594 namespace km 595 { 596 int n,head[10010],next[100010],zhi[100010],w[100010]; 597 int lx[10010],ly[10010],pre[10010],s[10010],t[10010]; 598 bool find(int x,int T) 599 { 600 s[x]=T; 601 for(int i=head[x];i;i=next[i]) 602 if(lx[x]+ly[zhi[i]]==w[i]&&t[zhi[i]]!=T) 603 { 604 t[zhi[i]]=T; 605 if(!pre[zhi[i]]||find(pre[zhi[i]],T)) 606 { 607 pre[zhi[i]]=x; 608 return 1; 609 } 610 } 611 return 0; 612 } 613 void maintain(int T) 614 { 615 int a=1<<30; 616 re(i,1,n)if(s[i]==T) 617 for(int j=head[i];j;j=next[j])if(t[zhi[j]]!=T) 618 a=min(a,lx[i]+ly[zhi[j]]-w[j]); 619 re(i,1,n) 620 { 621 if(s[i]==T)lx[i]-=a; 622 if(t[i]==T)ly[i]+=a; 623 } 624 } 625 void km() 626 { 627 re(i,1,n) 628 { 629 pre[i]=lx[i]=ly[i]=0; 630 for(int j=head[i];j;j=next[j]) 631 lx[i]=max(lx[i],w[j]); 632 } 633 for(int i=1,T=1;i<=n;i++)for(;;) 634 if(find(i,++T))break;else maintain(T); 635 } 636 } 637 namespace twosat 638 { 639 int n,head[200020],next[400040],zhi[400040],ed,s[400040],c; 640 bool bo[200020]; 641 bool dfs(int x) 642 { 643 if(bo[x^1])return 0; 644 else if(bo[x])return 1; 645 bo[x]=1,s[c++]=x; 646 for(int i=head[x];i;i=next[i]) 647 if(!dfs(zhi[i]))return 0; 648 return 1; 649 } 650 void add(int a,int b) 651 { 652 next[++ed]=head[a],head[a]=ed,zhi[ed]=b; 653 } 654 void add(int x,int vx,int y,int vy) 655 { 656 x=x*2+vx,y=y*2+vy; 657 add(x^1,y),add(y^1,x); 658 } 659 bool solve() 660 { 661 for(int i=0;i<n<<1;i+=2)if(!bo[i]&&!bo[i+1]) 662 { 663 c=0; 664 if(!dfs(i)) 665 { 666 while(c>0)bo[s[--c]]=0; 667 if(!dfs(i+1))return 0; 668 } 669 } 670 return 1; 671 } 672 void in() 673 { 674 inin(n); 675 re(i,0,n<<1)head[i]=0; 676 Clear(bo,0); 677 } 678 } 679 namespace zhuliu 680 { 681 const int inf=2147483647; 682 struct bian 683 { 684 int a,b,v; 685 bian(int a=0,int b=0,int v=0):a(a),b(b),v(v){} 686 }; 687 int n,m; 688 bian bi[40040]; 689 int bo[1010],pre[1010],in[1010],id[1010]; 690 void init(int x,int y){n=x;m=y;} 691 void add(int a,int b,int c,int i){bi[i].a=a,bi[i].b=b,bi[i].v=c;} 692 int solve(int root) 693 { 694 int ret=0; 695 while(1) 696 { 697 re(i,1,n)in[i]=inf; 698 re(i,1,m) 699 { 700 int u=bi[i].a,v=bi[i].b; 701 if(bi[i].v<in[v]&&u!=v)in[v]=bi[i].v,pre[v]=u; 702 } 703 re(i,1,n)if(i!=root&&in[i]==inf)return -1; 704 int sum=0; 705 Clear(id,0); 706 Clear(bo,0); 707 in[root]=0; 708 re(i,1,n) 709 { 710 ret+=in[i]; 711 int v=i; 712 while(bo[v]!=i&&!id[v]&&v!=root)bo[v]=i,v=pre[v]; 713 if(v!=root&&!id[v]) 714 { 715 sum++; 716 for(int u=pre[v];u!=v;u=pre[u]) 717 id[u]=sum; 718 id[v]=sum; 719 } 720 } 721 if(!sum)break; 722 re(i,1,n)if(!id[i])id[i]=++sum; 723 re(i,1,m) 724 { 725 int v=bi[i].b; 726 bi[i].a=id[bi[i].a]; 727 bi[i].b=id[bi[i].b]; 728 if(bi[i].a!=bi[i].b)bi[i].v-=in[v]; 729 } 730 n=sum; 731 root=id[root]; 732 } 733 return ret; 734 } 735 } 736 int main() 737 { 738 return 0; 739 }