算法详见:http://www.cnblogs.com/lokiii/p/8191573.html

求出点两两之间的最小割之后,把他们扔到map/set里跑即可

可怕的是map和set跑的时间竟然完全一样…代码里注释掉的部分是map

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int N=1005,M=100005,inf=1e9;
int T,n,m,Q,a[N],h[N],cnt=1,s,t,q[N],le[N],ans[N][N],sum;
bool v[N];
map<int,bool>mp;
set<int>st;
struct qwe
{
int ne,to,va;
}e[M<<1];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
bool bfs()
{
queue<int>q;
memset(le,0,sizeof(le));
le[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h[u];i;i=e[i].ne)
if(e[i].va>0&&!le[e[i].to])
{
le[e[i].to]=le[u]+1;
q.push(e[i].to);
}
}
return le[t];
}
int dfs(int u,int f)
{
if(u==t||!f)
return f;
int us=0;
for(int i=h[u];i&&us<f;i=e[i].ne)
if(e[i].va>0&&le[e[i].to]==le[u]+1)
{
int t=dfs(e[i].to,min(f-us,e[i].va));
e[i].va-=t;
e[i^1].va+=t;
us+=t;
}
if(!us)
le[u]=0;
return us;
}
int dinic()
{
int re=0;
while(bfs())
re+=dfs(s,inf);
return re;
}
void dfs(int u)
{
v[u]=1;
for(int i=h[u];i;i=e[i].ne)
if(e[i].va>0&&!v[e[i].to])
dfs(e[i].to);
}
void fen(int l,int r)
{
if(l==r)
return;
for(int i=2;i<=cnt;i+=2)
e[i].va=e[i^1].va=(e[i].va+e[i^1].va)>>1;
s=a[l],t=a[r];
int tmp=dinic();
memset(v,0,sizeof(v));
dfs(s);
for(int i=1;i<=n;i++)
if(v[i])
for(int j=1;j<=n;j++)
if(!v[j])
ans[i][j]=ans[j][i]=min(ans[i][j],tmp);
int ll=l,rr=r;
for(int i=l;i<=r;i++)
{
if(v[a[i]])
q[ll++]=a[i];
else
q[rr--]=a[i];
}
for(int i=l;i<=r;i++)
a[i]=q[i];
fen(l,ll-1);
fen(rr+1,r);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
ans[i][j]=inf;
for(int i=1;i<=n;i++)
a[i]=i;
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),w=read();
add(u,v,w);
add(v,u,w);
}
fen(1,n);
// for(int i=1;i<=n;i++)
// for(int j=i+1;j<=n;j++)
// if(!mp[ans[i][j]])
// {
// sum++;
// mp[ans[i][j]]=1;
// }
// printf("%d\n",sum);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
st.insert(ans[i][j]);
printf("%d\n",st.size());
return 0;
}

bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】的更多相关文章

  1. 最小割树Gomory–Hu tree

    fanhq666地址:http://fanhq666.blog.163.com/blog/static/8194342620113495335724/ wiki地址(证明):https://en.wi ...

  2. BZOJ 4519 [CQOI2016]不同的最小割

    这道题目很奇怪. 为什么奇怪?因为这道题用了一种叫分治最小割/最小割树的玩意. 以前从来没有见过这东西. 推荐一个讲这玩意的博客 写起来还是很顺手的. #include<iostream> ...

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

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

  4. bzoj 2229: [Zjoi2011]最小割【Gomory–Hu tree最小割树】

    这个算法详见http://www.cnblogs.com/lokiii/p/8191573.html 求出两两之间最小割之后暴力统计即可 #include<iostream> #inclu ...

  5. BZOJ 4520 [Cqoi2016]K远点对(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...

  6. [学习笔记]最小割树(Gomory-Hu Tree)

    最小割树(\(\mathcal{Gomory-Hu Tree}\))简明指南 对于单源最短路径,我们有\(SPFA\)和\(Dijkstra\),对于多源最短路径,我们有\(Floyd\):对于两点间 ...

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

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

  8. LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree

    2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  9. 【BZOJ-4519】不同的最小割 最小割树(分治+最小割)

    4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 393  Solved: 239[Submit][Stat ...

随机推荐

  1. CSS 遮罩层、滑出页面

    <style> .panel_bak { position:fixed; bottom:0; display:none; width:100%; margin:0px; padding:5 ...

  2. Spring教程:tutorialspoint-spring

    来自turorialspoint的Spring教程(英文),官网:https://www.tutorialspoint.com/spring/index.htm 这个教程在国内已经被翻译成中文(不过是 ...

  3. Spring MVC的Hello World例子

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-mvc-hello-world-example.html: ...

  4. openstack DVR的AIO 问题

    问题描述 : 创建public 网络,创建路由器,并且把路由器的gateway 设置指向网络后有下面几种错误 路由器对应的linux network namespace 建立起来了,但是里面并没有对应 ...

  5. [教程]Delphi 中三种回调函数形式解析

    Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...

  6. [NPM] npm check to update the dependencies

    To update the dependencies in the project, we can run: npx npm-check -u

  7. Hadoop架构设计、执行原理具体解释

    1.Map-Reduce的逻辑过程 如果我们须要处理一批有关天气的数据.其格式例如以下: 依照ASCII码存储.每行一条记录 每一行字符从0開始计数,第15个到第18个字符为年 第25个到第29个字符 ...

  8. 火狐浏览器Firefox 如何使用iMacros 自动填写网页表单

    1 我们首先访问一个想要自动填写表单的网站.我们以百度为例,右侧有登录窗口.   2 然后我们点开刚安装上的iMacros插件,一般安装之后就会自动出现在浏览器的某个地方,点击记录选项卡,再点击记录. ...

  9. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  10. bootstrap中固定table的表头

    前端时间鼓捣的一个简单的手机站点,今天又拿出来弄一弄 由于主要是给手机訪问.用的是bootstrap响应式布局,今天的任务是做一个数据展示页面 可是因为数据的列比較多.所以横向显示不下,为了较好的显示 ...