
图论
文章平均质量分 56
hao_zong_yin
有问题可以加QQ讨论2987048728,备注一下优快云
展开
-
UVA - 753 A Plug for UNIX——二分图最大基数匹配
用了ek最大流+flody闭包,然后瞎写一通#include #include #include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1e5;struct Data { string s; int原创 2017-10-07 21:49:16 · 303 阅读 · 0 评论 -
The Accomodation of Students 二分图判定+匹配
模板题,复习一下模板#include #include #include #include using namespace std;const int maxn = 205;int n, m, tot, head[maxn], vis[maxn], match[maxn];struct Edge { int to, next; } edges[maxn*maxn];void i原创 2017-12-19 19:53:07 · 271 阅读 · 0 评论 -
ZOJ 1324 Reactor Cooling——无源汇有上下界的可行流
#include #include #include #include #include #include using namespace std;const int maxn = 410;const int INF = 0x3f3f3f3f;int a[maxn], down[maxn*maxn];struct Edge { int from, to, flow, c原创 2017-12-20 22:08:10 · 304 阅读 · 0 评论 -
POJ 3255 Roadblocks——次短路
这个博主写得很清楚#include #include #include #include #include #include using namespace std;typedef pair P;const int maxn = 1e4;const int maxm = 250000;const int INF = 0x3f3f3f3f;int n, m, tot, he原创 2018-02-04 10:55:42 · 289 阅读 · 0 评论 -
POJ 3723 Conscription——最小生成树
边权取反,跑一遍MST,然后加上10000*(n+m),注意编号问题#include #include #include #include using namespace std;const int maxn = 50005;struct Edge { int u, v, cost; bool operator < (const Edge &e) const {原创 2018-02-04 12:09:31 · 297 阅读 · 0 评论 -
POJ 2976 Dropping tests——最大化平均值
注意n=k=0时break#include #include #include #include using namespace std;const int maxn = 1010;int n, k, a[maxn], b[maxn];double y[maxn];bool judge(double t) { for (int i = 1; i <= n; i++)原创 2018-02-07 21:21:53 · 264 阅读 · 0 评论 -
HDU - 6026 Deleting Edges——最短路计数
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef pair<int, int> P原创 2018-04-13 17:29:08 · 193 阅读 · 0 评论 -
HDU 4309 Seikimatsu Occult Tonneru——最大流
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;const int maxn = 200;const in...原创 2018-04-30 18:49:52 · 211 阅读 · 0 评论 -
HDU - 6031 Innumerable Ancestors——lca
把集合b按dfs序排序,然后对于集合a的每个元素a【i】对应的dfs序id【i】,在b中二分与他最接近的那个值(用lower_bound的话要判一下当前位置和当前位置-1看看哪个更接近),然后做lca求最大值即可#include <cstdio>#include <cstring>#include <iostream>#include <algori...原创 2018-04-14 00:38:00 · 193 阅读 · 0 评论 -
HDU 5883 HDU - 5883——欧拉路
注意判一下欧拉回路的情况,这种情况下需要找一个点再异或一次#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 1e5 + 10;const int maxm = 1...原创 2018-08-24 13:39:49 · 239 阅读 · 0 评论 -
有上下界的网络流
转自http://blog.youkuaiyun.com/regina8023/article/details/45815023写在前面有上下界的网络流即对边的流量有限制,必须在[down,up]的范围内。其实普通的网络流也是一种特殊的有上下界的网络流,只是每条边的流量限制为[0,cap]。分类有上下界的网络流分为两种:有源汇 所有点都要求满足流量平转载 2017-12-13 17:28:51 · 472 阅读 · 2 评论 -
CodeForces - 472D Design Tutorial: Inverse the Problem——最小生成树
#include #include #include #include using namespace std;typedef long long ll;const int maxn = 2005;struct Edge { int from, to, cost; bool operator < (const Edge &temp) const { r原创 2017-11-15 00:04:20 · 304 阅读 · 0 评论 -
UVA 11082 Matrix Decompressing——抽象建图+最大流
#include #include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 500;struct Edge { int from, to, cap, flow; Edge(int u, int v, int原创 2017-10-08 11:45:38 · 259 阅读 · 0 评论 -
UVA 1658 Admiral——拆点法+最小费最大流
对于边u->v的处理方法是先建立边u->u+n,权值为0, 然后建立边u+n->v权值为c,这样可以避免一个点经过多次的情况#include #include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 2 * 1e3 +原创 2017-10-08 16:19:41 · 351 阅读 · 0 评论 -
UVA 1349 Optimal Bus Route Design——最小权完美匹配
对于边u->v,将u拆分成u1 u2, v拆分成v1 v2,然后u1连接v2,s连接u1,v2连接t,就把整个图转化成了一个二分图,然后用二分图的最小权完美匹配就可以解决问题#include #include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;co原创 2017-10-08 20:48:56 · 396 阅读 · 0 评论 -
HihoCoder - 1121 二分图判定
#include #include #include #include using namespace std;const int maxn = 1e5 + 10;const int maxm = 4 * 1e5 + 10;int tot;int head[maxn];int vis[maxn];struct Edge { int to, next;}edge[max原创 2017-10-12 11:53:37 · 310 阅读 · 0 评论 -
匈牙利算法模板
#include #include #include #include using namespace std;const int maxn = 100;int n1, n2, m;bool vis[maxn];int match[maxn];vector G[maxn];bool dfs(int u) { for (int i = 0; i < G[u].size(原创 2017-10-12 20:02:13 · 244 阅读 · 0 评论 -
HDU 2444 The Accomodation of Students——二分图判定+最大匹配
判定后用匈牙利算法求出最大匹配#include #include #include #include #include using namespace std;const int maxn = 500;int n, m;vector G[maxn];int vis[maxn];int s1[maxn], s2[maxn], n1, n2;bool used[maxn];原创 2017-10-13 08:06:09 · 295 阅读 · 0 评论 -
HDU - 5521 Meeting——增点建边+最短路
#include #include #include #include #include #include using namespace std;typedef pair P;const long long INF = 1e18;const int maxn = 2 * 1e6;bool vis[maxn];int n, m;long long dis1[maxn], d原创 2017-10-16 13:39:31 · 357 阅读 · 0 评论 -
UVALive - 7008 Tactical Multiple Defense System——二分图最大匹配
一个点由半径和斜率确定,判重后将半径和斜率之间建一条边跑匈牙利即可#include #include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 2 * 1e4 + 10;int tot, head[maxn];str原创 2017-10-30 14:52:13 · 349 阅读 · 0 评论 -
UVA 10457 Magic Car——最小瓶颈路
其实就是瞎搞, 二重循环,第一层枚举最小值,第二层从最小值开始跑MST,当起点和终点在一个集合中时停止,此时求得的边权就是最大值,然后更新结果就好了PS:第一次刷到vjudge时间第一#include #include #include #include using namespace std;const int maxn = 205;const int maxm = 1005原创 2017-11-08 09:23:33 · 460 阅读 · 1 评论 -
HDU 5917 Instability——稳定子图
大于等于6个点的图都是稳定子图,其余暴力特判就可以5层循环也就2e6的复杂度#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int ma...原创 2018-09-14 19:18:20 · 249 阅读 · 0 评论 -
CodeForces - 787D Legacy——线段树优化建图
建两棵线段树,一棵表示入区间,一棵表示出区间入区间每个父亲和儿子连边,权值为0,出区间每个儿子和父亲连边,权值为0,入区间和相应的出区间连边,权值为0每次查询新建两个点,这里设为q1,q2,并假设要连边的两个区间为【l1,r1】,【l2,r2】(题目中为有向边),那么其实是【l1,r1】的出区间连q1,边权为0, q1连q2,边权为输入的边权,q2连【l2,r2】的入边,边权为0,和区间连...原创 2018-10-05 20:35:33 · 717 阅读 · 0 评论 -
网络流24题——1736: 飞行员配对方案问题——二分图匹配
描述:第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员。由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1 名是外籍飞行员。在众多的飞行员中,每一名外籍飞行员都可以与其他若干名英国飞行员很好地配合。如何选择配对飞行的飞行员才能使一次派出最多的飞机。对于给定的外籍飞行员与英国飞行员的配合情况,试设计一个算法找出最佳飞行员配对方...原创 2018-07-15 15:20:45 · 359 阅读 · 0 评论 -
洛谷P2863[USACO06JAN]牛的舞会The Cow Prom——强连通分量
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>using namespace std;const int maxn = 1e5 + 10;vector<int> G[maxn]...原创 2018-08-10 00:21:32 · 231 阅读 · 0 评论 -
POJ 2186 Popular Cows——Tarjan缩点
题意:有n头牛,m个崇拜关系,并且崇拜具有传递性,如果a崇拜b,b崇拜c,则a崇拜c,求最后有几头牛被所有牛崇拜。思路:显然一个强联通分量内的所有点都是满足条件的,我们可以对整张图进行缩点,然后就简单了。剩下的所有点都不是强连通的,现在整张图就是一个DAG(有向无环图)那么就变成一道水题了,因为这是一个有向无环图,不存在所有点的出度都不为零的情况。所以必然有1个及以上的点出度为...原创 2018-08-10 01:13:20 · 234 阅读 · 0 评论 -
UVALive 5135 Mining Your Own Business——点双连通分量
https://blog.youkuaiyun.com/l123012013048/article/details/47347023点数可能大于边数,导致用n初始化不够,TLE了两发,改了memset过了#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5 + 10...原创 2018-08-10 20:21:01 · 181 阅读 · 0 评论 -
POJ 3352 Road Construction——边双连通分量
至少增加的边数 =( 缩点后树总度数为1的结点数 + 1 )/ 2#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>using namespace std;const int maxn= 1...原创 2018-08-11 10:10:33 · 168 阅读 · 0 评论 -
HDU 5458 Stability——双连通缩点+树链剖分
思路:首先将所有1操作的边都删去,然后倒着查询,遇到1操作就增边,最后倒着输出删去所有1操作后,我们对整个图进行双连通缩点,将图变成一棵树,所有边权都为1,查询的时候就是查询树上区间的和,更新便是将两点之间路径的边权全部变为0。这里可以用树链剖分+线段树做,注意一个小技巧,那就是我们可以用点权代表这个点与他父节点之间的边的边权,更新和查询的具体操作看树链剖分部分的代码吧。#include...原创 2018-08-12 10:25:02 · 293 阅读 · 3 评论 -
HDU 6437 Problem L.Videos——费用流
s到每个人容量为1,费用为0的边每个人到每个视频建容量为1, 费用为0的边每个视频拆点,建容量为1,费用为-w的边视频之间若时间不覆盖,则容量为1的边,若两视频op相同,费用为W, 否则为0每个视频到t建容量为1, 费用为0的边跑一遍s-t最小费用流,答案取负即可#include <cstdio>#include <cstring>#inclu...原创 2018-08-22 21:25:20 · 302 阅读 · 0 评论 -
斯坦纳树——hdu 4085
最近打全国高校绿色计算机大赛遇到了一个斯坦纳树(决赛第二阶段第三题),当时网上找了模板水过去了,赛后学习一下【一】什么是斯坦纳树斯坦纳树问题是组合优化学科中的一个问题。将指定点集合中的所有点连通,且边权总和最小的生成树称为最小斯坦纳树(Minimal Steiner Tree),其实最小生成树是最小斯坦纳树的一种特殊情况。而斯坦纳树可以理解为使得指定集合中的点连通的树,但不一定最小。【...原创 2018-11-12 20:32:57 · 499 阅读 · 0 评论 -
Codeforces 1076D Edge Deletion——最短路+dfs
题意:n个点m条边的无向连通图(无自环、重边),现在要删除一些边使得图最多剩下k条边,并且这样的点尽量多:这个点到1号节点的最短路不变思路:建出最短路树,统计边数,然后跑一遍dfs,在回溯的时候判断边数与k的关系,若边数>k就使边数减一,否则记录答案#include <bits/stdc++.h>using namespace std;const int m...原创 2018-11-19 10:12:29 · 463 阅读 · 0 评论 -
Codeforces 1131D Gourmet choice——差分约束
题意:给定一张比较表,给行列赋值使得比较表成立,并且最大值最小思路:差分约束,求解最长路/*** x > y : x >= y + 1;* x < y : x <= y - 1;* x = y : x >= y x <= y* maxlongpath: u -> v :* if (dis[v] < dis[u] + cost) d...原创 2019-03-09 21:34:51 · 579 阅读 · 0 评论 -
1744: 方格取数问题——最小割
题意:在一个有m*n 个方格的棋盘中,每个方格中有一个正整数。现要从方格中取数,使任 意2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。 编程任务: 对于给定的方格棋盘,按照取数要求编程找出总和最大的数。思路:首先对矩阵进行黑白点染色, 相邻的两个点颜色不同。然后每个黑点向四周连有向边,容量为INF, 之后源点向黑点连有向边,容量为点权,白点向汇点连边,...原创 2018-07-21 17:56:18 · 263 阅读 · 0 评论 -
1742: 试题库问题——二分图多重匹配
题意:假设一个试题库中有n道试题。每道试题都标明了所属类别。同一道题可能有多个类别 属性。现要从题库中抽取m 道题组成试卷。并要求试卷包含指定类型的试题。试设计一个 满足要求的组卷算法。 编程任务: 对于给定的组卷要求,计算满足要求的组卷方案。思路:建图直接跑就行,水题#include <cstdio>#include <cstring>#include &l...原创 2018-07-21 13:32:22 · 222 阅读 · 0 评论 -
ZOJ 2314 Reactor Cooling——无源汇有上下界可行流
题意:判断一个图时候能构成无源汇有上下界可行流,是的话输出YES,并输出每条边的流量,不是的话输出NO思路:https://www.cnblogs.com/liu-runda/p/6262832.html#include <cstdio>#include <cstring>#include <iostream>#include <algor...原创 2018-07-15 19:28:00 · 200 阅读 · 0 评论 -
ZOJ 3229 Shoot the Bullet——有源汇有上下界最大流
描述:文文要给幻想乡中的m个人拍照,每天要对给定的C个人拍照(比如要给灵梦、魔理沙、紫adsfas拍照),每天拍照总数不能超过D[i]张,并且每天给每个人的拍照数要在【L, R】内,经过n天的拍照后,给每个人的拍照总数不能少于G[i],否则文文就会遭到大家终符的攻击。在上面的限制下问文文最多能拍多少张照片,并且输出每天分别给这天指定的C个人拍的照片数,如果文文无法避免终符的攻击,输出-1思路:...原创 2018-07-16 16:04:36 · 228 阅读 · 0 评论 -
POJ 3801 Crazy Circuits——有源汇有上下界最小流
题意:给定n个点m条边,点的编号为1~n,另外有两个特殊的点+和-,+为原点,-为汇点,给定每条边的下限,问这个网络的最小流是多少?思路:有源汇有有下界的最小流,详解:https://www.cnblogs.com/liu-runda/p/6262832.html#include <cstdio>#include <cstring>#include <i...原创 2018-07-17 10:48:56 · 264 阅读 · 0 评论 -
HYSBZ 3876 支线剧情——有上下界的费用流
题意:中文题意,总而言之就是每条边都要被经过思路:把需要被覆盖的边的下界设为1,然后根据题意每个点(除1)向1连边,这样就形成了一个无源汇的图,首先给每条边一个流量,值为它的下界,构成一个初始流,求这个初始流的费用,然后 在这个图的残余网络上建立ss和tt跑费用流,求得的值加上之前初始流的值就是结果#include <cstdio>#include <cstring&...原创 2018-07-17 18:00:16 · 257 阅读 · 0 评论 -
1737: 太空飞行计划问题——最大权闭合子图
题意:W 教授正在为国家航天中心计划一系列的太空飞行。每次太空飞行可进行一系列商业 性实验而获取利润。现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这 些实验需要使用的全部仪器的集合I={I1,I2,…In}。实验Ej需要用到的仪器是I的子集Rj。 配置仪器Ik的费用为ck美元。实验Ej的赞助商已同意为该实验结果支付pj美元。W教授的 任务是找出一个有效算法,确定在一次太...原创 2018-07-20 09:49:56 · 210 阅读 · 0 评论