BZOJ4435——[Cerc2015]Juice Junctions
0、题目大意:求两点之间的最小割之和
1、分析:很明显,最小割树,我们发现这个题并不能用n^3的方法来求答案。。
所以我们记录下所有的边,然后把边从大到小排序,然后跑一边类似kruskal的东西,顺便统计答案
TAT,这个题我被卡常数了,贴上TLE的代码吧。。。
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 214748364
struct Edge{
int from, to, cap, flow, next;
};
int head[3010], cur[3010];
Edge G[20010];
int tot;
int d[3010];
bool vis[3010];
int s, t, n, m;
int a[3010];
int b[3010];
inline void init(){
memset(head, -1, sizeof(head));
tot = -1;
return;
}
inline void insert(int from, int to, int cap){
G[++ tot] = (Edge){from, to, cap, 0, head[from]};
head[from] = tot;
G[++ tot] = (Edge){to, from, 0, 0, head[to]};
head[to] = tot;
return;
}
inline bool BFS(){
for(int i = 1; i <= n; i ++) vis[i] = 0;
queue<int> Q;
Q.push(s);
vis[s]=1;
d[s]=0;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = head[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(e.cap - e.flow > 0 && !vis[e.to]){
vis[e.to] = 1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
inline int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0){
e.flow += f;
G[i ^ 1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
inline int maxflow(){
int res = 0;
while(BFS()){
for(int i = 1; i <= n; i ++) cur[i] = head[i];
res += dfs(s, inf);
}
return res;
}
inline void Clear(){
for(int i = 0; i <= tot; i += 2){
G[i].flow = G[i ^ 1].flow = (G[i].flow + G[i ^ 1].flow) / 2;
}
}
struct node{
int u, v, w;
inline bool operator < (const node& rhs) const{
return w > rhs.w;
}
} T[20010];
int num;
inline void add(int u, int v, int w){
T[++ num].u = u;
T[num].v = v;
T[num].w = w;
}
inline void solve(int l, int r){
if(l == r) return;
int rl = rand() % (r - l + 1) + l;
int rr = rand() % (r - l + 1) + l;
if(rl == rr) rl ++;
if(rl > r) rl -= 2;
s = a[rl], t = a[rr];
Clear();
int tw = maxflow();
//puts("fuck");
add(a[rl], a[rr], tw);
int L = l, R = r;
for(int i = l; i <= r; i ++){
if(vis[a[i]]) b[L ++] = a[i];
else b[R --] = a[i];
}
for(int i = l; i <= r; i ++) a[i] = b[i];
solve(l, L - 1); solve(L, r);
}
int ff[20010], size[20010];
inline int find(int x){
return ff[x] == x ? x : ff[x] = find(ff[x]);
}
int main(){
// srand(time(NULL));
scanf("%d%d", &n, &m);
init();
for(int i = 1; i <= m; i ++){
int u, v;
scanf("%d%d", &u, &v);
insert(u, v, 1); insert(v, u, 1);
}
for(int i = 1; i <= n; i ++) a[i] = i;
solve(1, n);
LL ret = 0;
sort(T + 1, T + num + 1);
for(int i = 1; i <= n; i ++) size[i] = 1, ff[i] = i;
// puts("fuck");
for(int i = 1; i <= num; i ++){
int tx = find(T[i].u), ty = find(T[i].v);
if(size[tx] < size[ty]) swap(tx, ty);
ret += (LL)T[i].w * size[tx] * size[ty];
ff[ty] = tx;
size[tx] += size[ty];
}
printf("%lld\n", ret);
return 0;
}
BZOJ4435——[Cerc2015]Juice Junctions的更多相关文章
- BZOJ4435 : [Cerc2015]Juice Junctions
最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...
- bzoj4435: [Cerc2015]Juice Junctions(最小割树+hash)
传送门 首先最大流等于最小割,那么可以转化为最小割树来做(不知道什么是最小割树的可以看看这题->这里) 具体的做法似乎是$hash[i][j]$表示最小割为$i$时点$j$是否与$S$连通 然后 ...
- 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash
[BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...
- 【BZOJ-4435】Juice Junctions 最小割树(分治+最小割)+Hash
4435: [Cerc2015]Juice Junctions Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 20 Solved: 11[Submi ...
- BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash
分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...
- [CERC2015]Juice Junctions(边双连通+字符串hash)
做法 考虑边数限制的特殊条件,显然答案仅有\(\{0,1,2,3\}\) 0:不联通 1:连通 2:边双连通 3:任意删掉一条边都为边双连通 考虑每次删边后记录各点的边双染色情况来特判\(3\):是否 ...
- Juice Junctions
Juice Junctions 题目描述 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都是11升每秒.管道可能连接节点,每个节点最多可以连接3 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Gym - 101480 CERC 15:部分题目题解(队内第N次训练)
-------------------题目难度较难,但挺有营养的.慢慢补. A .ASCII Addition pro:用一定的形式表示1到9,让你计算加法. sol:模拟. solved by fz ...
随机推荐
- JavaWeb---总结(五)Http协议
一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...
- NXP Mifare S50标准IC卡- 访问位(Access Bits) 分析
Mifare S50 标准IC卡有1K 字节的EEPROM,主要用来存储数据和控制信息.1K 字节的EEPROM分成16 个区,每区又分成4 段,每1段中有16 个字节.每个区的最后一个段叫“尾部&q ...
- linq 之 Distinct的使用
public class Product { public string Name { get;set;} public int Code { get; set; } } class ProductC ...
- 菜单each+hover
<script type="text/javascript"> $(document).ready(function(){ var src1= new Array('i ...
- JabRef 文献管理软件
JabRef 文献管理软件简明教程 大多只有使用LaTeX撰写科技论文的研究人员才能完全领略到JabRef的妙不可言,但随着对Word写作平台上BibTeX4Word插件的开发和便利应用,使用Word ...
- OC-protocol
一.简单使用 1. 基本用途 可以用来声明一大堆方法(不能声明成员变量) 只要某个类遵守了这个协议,就相当于拥有这个协议中的所有方法声明 只要父类遵守了某个协议,就相当于子类也遵守了 2. 格式 协议 ...
- C++基础入门
#include "iostream" using namespace std; class A{ public: A(int x1){ x = x1; } ...
- JSTL I18N 格式标签库
<%@ page language="java" pageEncoding="gbk"%> <%@ taglib prefix="c ...
- 让Xcode的控制台支持LLDB类型的打印
这个技巧个人认为非常有用 当Xcode在断点调试的时候,在控制台中输入 po self.view.frame 类似这样的命令会挂掉,不信可以亲自去试试(Xcode7 以后支持LLDB类型的打印) 那么 ...
- 解决DWZ(JUI)的panel 点击关闭或者打开按钮 自己写的标签消失
问题描述:DWZ的panel面板比较常用,我们常常需要在其标题栏上再增加一个些按钮,如下图问题出来了,增加按钮后,点面板收缩按钮,增加的按钮就消失了而且面板收缩的click事件,也跟新增的按钮绑定了, ...