这道题目很奇怪.

为什么奇怪?因为这道题用了一种叫分治最小割/最小割树的玩意.

以前从来没有见过这东西.

推荐一个讲这玩意的博客

写起来还是很顺手的.

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<string>
#include<iomanip>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define FILE "dealing"
#define up(i,j,n) for(int i=j;i<=n;++i)
#define db double
#define uint unsigned ll
#define eps 1e-12
#define pii pair<ll,ll>
ll read(){
ll x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f*x;
}
const ll maxn=20500,limit=1e6,mod=(ll)(7+1e9+0.1);
const ll inf=(ll)(1e9);
template<class T>bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T>bool cmin(T& a,T b){return a>b?a=b,true:false;}
template<class T>T min(T& a,T& b){return a<b?a:b;}
template<class T>T max(T& a,T& b){return a>b?a:b;}
int n,m,S,T,id[maxn],vis[maxn],tmp[maxn],w[maxn],tot=0;
struct node{
int y,next,flow,rev;
}e[maxn];
int len=1,linkk[maxn];
void insert(int x,int y,int flow){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
e[len].flow=flow;
e[len].rev=len+1;
e[++len].y=x;
e[len].next=linkk[y];
linkk[y]=len;
e[len].flow=flow;
e[len].rev=len-1;
}
int d[maxn],q[maxn],head,tail;
bool makelevel(){
up(i,1,n)d[i]=-1;
d[S]=0;head=tail=0;
q[++tail]=S;
while(++head<=tail){
int x=q[head];
for(int i=linkk[x];i;i=e[i].next)
if(d[e[i].y]==-1&&e[i].flow)d[e[i].y]=d[x]+1,q[++tail]=e[i].y;
}
return d[T]!=-1;
}
int makeflow(int x,int flow){
if(x==T||!flow)return flow;
int maxflow=0,dis=0;
for(int i=linkk[x];i&&maxflow<flow;i=e[i].next){
if(d[e[i].y]==d[x]+1&&e[i].flow)
if(dis=makeflow(e[i].y,min(flow-maxflow,e[i].flow))){
e[i].flow-=dis;
e[i^1].flow+=dis;
maxflow+=dis;
}
}
if(!maxflow)d[x]=-1;
return maxflow;
}
int dinic(){
int ans=0,d;
while(makelevel())
while(d=makeflow(S,inf))
ans+=d;
return ans;
}
void dfs(int x){
vis[x]=1;
for(int i=linkk[x];i;i=e[i].next){
if(e[i].flow&&!vis[e[i].y])
dfs(e[i].y);
}
}
void divide(int l,int r){
if(l==r)return;
for(int i=2;i<=len;i+=2)
e[i].flow=e[i^1].flow=(e[i].flow+e[i^1].flow)>>1;
S=id[l],T=id[r];
int maxflow=dinic();
w[++tot]=maxflow;
up(i,1,n)vis[i]=0;
dfs(S);
int L=l,R=r;
up(i,l,r){
if(vis[id[i]])tmp[L++]=id[i];
else tmp[R--]=id[i];
}
up(i,l,r)id[i]=tmp[i];
divide(l,L-1);divide(R+1,r);
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(),m=read();
up(i,1,n)id[i]=i;
up(i,1,m){
int x=read(),y=read(),v=read();
insert(x,y,v);
}
divide(1,n);
sort(w+1,w+tot+1);
w[0]=-1;
int ans=0;
up(i,1,tot)if(w[i]!=w[i-1])ans++;
printf("%d\n",ans);
return 0;
}

  

BZOJ 4519 [CQOI2016]不同的最小割的更多相关文章

  1. bzoj 4519: [Cqoi2016]不同的最小割 最小割树

    怎么求一张无向图中任意两点之间的最小割? http://fanhq666.blog.163.com/blog/static/8194342620113495335724/ 一张无向图不同的最小割最多有 ...

  2. bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】

    算法详见:http://www.cnblogs.com/lokiii/p/8191573.html 求出点两两之间的最小割之后,把他们扔到map/set里跑即可 可怕的是map和set跑的时间竟然完全 ...

  3. 4519: [Cqoi2016]不同的最小割

    4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 489 Solved: 301 [Submit][Stat ...

  4. bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...

  5. bzoj4519: [Cqoi2016]不同的最小割(分治最小割)

    4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...

  6. [ZJOI2011]最小割 & [CQOI2016]不同的最小割 分治求最小割

    题面: [ZJOI2011]最小割 [CQOI2016]不同的最小割 题解: 其实这两道是同一道题.... 最小割是用的dinic,不同的最小割是用的isap 其实都是分治求最小割 简单讲讲思路吧 就 ...

  7. 【BZOJ4519】[Cqoi2016]不同的最小割 最小割树

    [BZOJ4519][Cqoi2016]不同的最小割 Description 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分 ...

  8. [BZOJ 3144] [Hnoi2013] 切糕 【最小割】

    题目链接:BZOJ - 3144 题目分析 题意:在 P * Q 的方格上填数字,可以填 [1, R] . 在 (x, y) 上填 z 会有 V[x][y][z] 的代价.限制:相邻两个格子填的数字的 ...

  9. [BZOJ 3894] 文理分科 【最小割】

    题目链接:BZOJ - 3894 题目分析 最小割模型,设定一个点与 S 相连表示选文,与 T 相连表示选理. 那么首先要加上所有可能获得的权值,然后减去最小割,即不能获得的权值. 那么对于每个点,从 ...

随机推荐

  1. bit、位、byte、字节、B、KB、字符与网速

    一.存储单位bit和Byte 1.bit(比特) bit就是位,也叫比特位,是数据存储的最小单位.简写为小写字母“b” 二进制的一位,每个0或1是一个bit 2.Byte(字节) Byte是字节,也有 ...

  2. java中日期格式的转换和应用

    java中主要有3个类用于日期格式转换    DateFormat .SimpleDateFormat.Calendar SimpleDateFormat函数的继承关系: java.lang.Obje ...

  3. 各类免费的API接口分享,无限次

    各类免费的API接口分享: 手机号码归属地API:https://www.juhe.cn/docs/api/id/11 历史上的今天API:https://www.juhe.cn/docs/api/i ...

  4. iptables和DNS

    1.iptables防火墙 表→链→规则 filter表 数据过滤表 NAT表---内网和外网的地址转换 Mangle-----数据流量,通过防火墙设置流量.特殊数据包标记.太复杂,一般不用.限速工具 ...

  5. TCP/IP详解 卷一(第二章 链路层)

    在TCP/IP协议族中,链路层主要有三个目的: 1.为IP模块发送和接收IP数据报 2.为ARP模块发送ARP请求和接收ARP应答 3.为RARP请求和接收RARP应答 TCP/IP支持多种不同的链路 ...

  6. mysql:4种时间类型

    insert 12 ================= 养成良好的习惯,除了整形和浮点型不加'',其余都加,包括日期时间型

  7. Mjpg_Streamer 的移植

    1. 移植mjpg-streamer a.1 移植libjpeg tar zxf libjpeg-turbo-1.2.1.tar.gz cd libjpeg-turbo-1.2.1 ./configu ...

  8. containsKey使用方法

    作用是判断Map中是否有所需要的键值,下面是具体的代码: public static void main(String[] args) { Map<String, String> map ...

  9. jquery元素分组插件,用于把一连串元素分成多组,如把多个a标签分成多组放入<li>元素中,可以用于简化多图滚动为一个元素滚动,兼容ie6

    三个参数 <script type="text/javascript"> /* *sclass:设置包裹元素的类 * packages:设置包裹的元素 * row:设置 ...

  10. Android应用开发:网络工具——Volley(一)

    引言 网络一直是我个人的盲点,前一阵子抽空学习了一下Volley网络工具的用法,也透过源代码进行了进一步的学习,有一些心得想分享出来.在Android开发中,成熟的网络工具不少,Android自带了H ...