图论
XiWenjuan086
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
HDU - 6290 奢侈的旅行(最短路)
思路:假设经过城市的顺序为1~n,花费为可以发现最终花费只与 a 有关,所以把 a 当边权跑最短路,在松弛操作时判断可不可走。#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef double db;const db eps = 1e-6;const int N = 1e5 + 7;const int M = 2e5 .原创 2021-10-16 11:43:11 · 164 阅读 · 0 评论 -
codeforces1454E. Number of Simple Paths(基环图 + 思维)
题意:给一个基环图(比一棵树多一条边的图),问图中原创 2021-06-02 19:58:31 · 192 阅读 · 0 评论 -
OpenJ_Bailian - 1679 The Unique MST (次小生成树模板)
Given a connected undirected graph, tell if its minimum spanning tree is unique.Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:1...原创 2021-03-31 20:21:05 · 159 阅读 · 0 评论 -
P4180 [BJWC2010]严格次小生成树(kruskal + 倍增 + lca)
思路:(1)先求最小生成树,重新建图(2)遍历所有非树边,用树上倍增求LCA的方法求出非树边两节点之间树边中的最大边和次大边,再将非树边权值与最大值比较,如果最大边<非树边(或者不等于,不等于一定<,要不然最小生成树就不是最小了)权值,用非树边替换最大边,否则(等于关系)用非树边替换次大边,最后从所有候选答案中选择最小值即次小生成树权值。参考:https://www.cnblogs.com/bianjunting/p/10829212.htmlhttps://43102...原创 2021-03-30 22:07:21 · 242 阅读 · 2 评论 -
牛客挑战赛48C-铬合金之声(prufer序列 + 思维)
传送门题意:给n 个点连 m 条边,使得刚好有n - m 个连通块,问所有方案中最大连通块大小乘积之和。贴个题解:purfer序列学习purfer序列的性质:(1)一棵n 个点的无根树共有种因为purfer序列中数唯一对应一棵树,对于purfer序列共有 n - 2个数,每个数可以是1-n中任何一个数,所以得到(2)purfer序列中每个点出现次数为该点在无根树中度数-1(3)一棵n 个点的树,purfer序列的长度为 n - 2(4)一棵树只有一个pu...原创 2021-03-22 22:00:21 · 206 阅读 · 0 评论 -
hdu1814 Peaceful Commission(2-SAT)
题意:n个公司,每个公司有两个代表,有些代表之间有矛盾,现在每个公司派一个代表参加会议,要求有矛盾的人不能同时参加会议,求字典序最小的解2-SAT裸题,求字典序最小只能dfs,u 和 u ^ 1不能同时选,若u、v有矛盾,则选u的话一定选v ^ 1,选v的话一定选u ^ 1#include <bits/stdc++.h>using namespace std;typedef long long ll;const int inf = 0x3f3f3f3f;const in..原创 2021-03-17 21:32:40 · 154 阅读 · 0 评论 -
poj3648 Wedding(2-SAT模板)
题意:新郎新娘邀请n - 1夫妻参加婚宴,桌子只有左右两边可坐,新娘新郎对坐在桌子两边,这 n - 1 对夫妻中有 m 个通奸关系,新娘不想在对桌看到任何一对夫妻或者有通奸关系的人,输出一种和新娘坐在同一侧的方式。分析:2-SAT维护和新郎在同侧的人,(a 与 a ^ 1是一对夫妻)如果两个人 u、v有通奸关系,选 u 的话一定要选 v ^ 1,选 v 的话一定要选 u ^ 1。首先要把新郎打上标记,因为新郎必选。染色法(得到字典序最小的解)#include <ios...原创 2021-03-17 21:23:26 · 150 阅读 · 0 评论 -
2020 ICPC Taipei H.Optimization for UltraNet(二分 + 最小生成树)
题意:好长啊求一棵生成树,首先满足最短边最大,其次使任意两点路径上的最短边之和最小。思路:要使最短边取最大值,考虑二分最短边,判断当前能否生成一棵树,如果可以,考虑使任意两点路径上的最短边之和最小,也就是说,其余的边应该尽量小,即求最小生成树。建完图之后跑一遍任意两点路径上的最短边和就可以了。参考#include<bits/stdc++.h>using namespace std;typedef long long ll;const double eps = 1e...原创 2021-01-22 00:41:54 · 427 阅读 · 0 评论 -
牛客编程巅峰赛S2第5场 - 钻石&王者 C Tree III(树的第二直径)
题意:求树的第二直径思路:树的第二直径一定以树的直径的某一端为端点,用两次dfs求树的直径的两个端点,再对两个端点分别进行dfs求出端点到其他点的距离,把这些距离从大到小排个序,第一第二大的都是直径,第三大的就是第二直径。思路参考https://blog.youkuaiyun.com/qq_43765333/article/details/110454816#include <iostream>#include <cstdio>#include <cmath>..原创 2020-12-03 21:35:11 · 178 阅读 · 0 评论 -
poj1985 Cow Marathon(树的直径)
Cow MarathonTime Limit:2000MS Memory Limit:30000K Total Submissions:9653 Accepted:4411 Case Time Limit:1000MS DescriptionAfter hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, ...原创 2020-12-03 20:07:10 · 493 阅读 · 0 评论 -
UVA - 10480 Sabotage (最小割 + 路径输出)
题意:给一个n点m条边的无向图,现在要删掉一些边使得点1、2不连通,每条边有一个花费,问如何删边思路:显而易见求最小割,也就是最大流,主要是如何输出路径引用自https://blog.youkuaiyun.com/ac_lion/article/details/8620676最小割,就是在所有割中,容量之和最小的割。最小割的值就是最大流的值,因为很容易想到,从源点s到汇点t的最大流必然会经过割边,那么就有最大流f<=c(割边的值),那么也就是说,当c==f的时候,就是c为小割,即最大流==最小割..原创 2020-11-18 19:11:06 · 213 阅读 · 0 评论 -
2020ICPC·小米 网络选拔赛第一场 B Intelligent Robot(建图 + 最短路)
链接:https://ac.nowcoder.com/acm/contest/7501/B来源:牛客网时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld题目描述Given a maze of size n×mn\times mn×m, whose lower-left corner is (0,0)(0, 0)_{}(0,0) while upper-right corner is (n,m).原创 2020-10-27 20:47:59 · 294 阅读 · 0 评论 -
2020ICPC·小米 网络选拔赛第一场 D - Router Mesh(求删掉割点后的连通块数)
链接:https://ac.nowcoder.com/acm/contest/7501/D来源:牛客网时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld题目描述In a Mesh networking system, there are nn_{}n MI Routers, where mm_{}m pairs of MI Routers are bidirectionally connec.原创 2020-10-25 20:22:12 · 312 阅读 · 0 评论 -
HDU - 1045 Fire Net (二分图 + 缩点)
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.A blockhouse is a small castle that has four openings through which to shoot. The four op.原创 2020-10-21 19:51:51 · 225 阅读 · 0 评论 -
HDU - 2444 The Accomodation of Students (二分图染色 + 最大匹配)
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.Now you are given all pairs of students who know ea...原创 2020-10-15 16:59:43 · 185 阅读 · 0 评论 -
POJ - 1236 Network of Schools (强连通分支 + 缩点)
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of s原创 2020-10-15 14:33:43 · 162 阅读 · 0 评论 -
luogu P2341 [USACO03FALL][HAOI2006]受欢迎的牛 G(强连通分支模板 + 缩点)
题目背景本题测试数据已修复。题目描述每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果AA喜欢BB,BB喜欢CC,那么AA也喜欢CC。牛栏里共有NN头奶牛,给定一些奶牛之间的爱慕关系,请你算出有多少头奶牛可以当明星。输入格式第一行:两个用空格分开的整数:NN和MM。接下来MM行:每行两个用空格分开的整数:AA和BB,表示AA喜欢BB。输出格式...原创 2020-10-14 21:59:14 · 221 阅读 · 0 评论 -
luogu P2766 最长不下降子序列问题(LIS + 最大流)
题目描述给定正整数序列x_1 \ldots, x_nx1…,xn。计算其最长不下降子序列的长度ss。 如果每个元素只允许使用一次,计算从给定的序列中最多可取出多少个长度为ss的不下降子序列。 如果允许在取出的序列中多次使用x_1x1和x_nxn(其他元素仍然只允许使用一次),则从给定序列中最多可取出多少个不同的长度为ss的不下降子序列。令a_1, a_2, \ldots, a_sa1,a2,…,as为构造SS时所使用的下标,b_1, b_2, \ldot...原创 2020-10-14 10:57:58 · 201 阅读 · 0 评论 -
luogu P2774 方格取数问题(最小割)
题目描述有一个mm行nn列的方格图,每个方格中都有一个正整数。现要从方格中取数,使任意两个数所在方格没有公共边,且取出的数的总和最大,请求出最大的和。输入格式第一行是两个用空格隔开的整数,分别代表方格图的行数mm和列数nn。第22到第(m + 1)(m+1)行,每行nn个整数,第(i + 1)(i+1)行的第jj个整数代表方格图第ii行第jj列的的方格中的数字a_{i, j}ai,j。输出格式输出一行一个整数,代表和最大是多少。输入输...原创 2020-10-08 17:57:49 · 389 阅读 · 0 评论 -
luogu P3356 火星探险问题(网络流24题 拆点 + 最小费用流 + 路径输出)
题目描述火星探险队的登陆舱将在火星表面着陆,登陆舱内有多部障碍物探测车。登陆舱着陆后,探测车将离开登陆舱向先期到达的传送器方向移动。探测车在移动中还必须采集岩石标本。每一块岩石标本由最先遇到它的探测车完成采集。每块岩石标本只能被采集一次。岩石标本被采集后,其他探测车可以从原来岩石标本所在处通过。探测车不能通过有障碍的地面。本题限定探测车只能从登陆处沿着向南或向东的方向朝传送器移动,而且多个探测车可以在同一时间占据同一位置。如果某个探测车在到达传送器以前不能继续前进,则该车所采集的岩石标本将全部损原创 2020-10-08 16:16:01 · 244 阅读 · 0 评论 -
luogu P4716 【模板】最小树形图
题目背景这是一道模板题。题目描述给定包含nn个结点,mm条有向边的一个图。试求一棵以结点rr为根的最小树形图,并输出最小树形图每条边的权值之和,如果没有以rr为根的最小树形图,输出-1−1。输入格式第一行包含三个整数n,m,rn,m,r,意义同题目所述。接下来mm行,每行包含三个整数u,v,wu,v,w,表示图中存在一条从uu指向vv的权值为ww的有向边。输出格式如果原图中存在以rr为根的最小树形图,就输出最小树形图每条边的权值之和,...原创 2020-10-07 21:49:30 · 150 阅读 · 0 评论 -
Gym - 102431 K(CCPC2019final DSU on tree)
Christmas is still one month away, but Mr. Panda already starts the Christmas preparation. Mr. Panda is decorating a Christmas tree with a set of Russian dolls. There arennRussian dolls numbered1,2,…,n1,2,…,n. Theithithdoll is designed to be perfec...原创 2020-10-06 20:51:54 · 458 阅读 · 1 评论 -
CodeForces - 1422D Returning Home (建图 + 最短路)
Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.Let's represent the city as an area ofn×nn×nsquare blocks. Yur..原创 2020-10-06 20:17:20 · 418 阅读 · 2 评论 -
luogu P3254 圆桌问题(网络流24题 带权二分图匹配)
题目描述有来自mm个不同单位的代表参加一次国际会议。第ii个单位派出了r_iri个代表。会议的餐厅共有nn张餐桌,第ii张餐桌可容纳c_ici个代表就餐。为了使代表们充分交流,希望从同一个单位来的代表不在同一个餐桌就餐。请给出一个满足要求的代表就餐方案。输入格式输入的第一行是用空格隔开的两个整数,分别代表单位的个数mm和餐桌的个数nn。第二行有mm个用空格隔开的整数,第ii个整数代表第ii个单位的代表人数r_iri。第三行有nn个...原创 2020-10-04 12:38:13 · 211 阅读 · 0 评论 -
luogu P2764 最小路径覆盖问题
题目描述给定有向图G=(V,E)G=(V,E)。设PP是GG的一个简单路(顶点不相交)的集合。如果VV中每个定点恰好在PP的一条路上,则称PP是GG的一个路径覆盖。PP中路径可以从VV的任何一个定点开始,长度也是任意的,特别地,可以为00。GG的最小路径覆盖是GG所含路径条数最少的路径覆盖。设计一个有效算法求一个 DAG (有向无环图)GG的最小路径覆盖。提示:设V=\{1,2,...,n\}V={1,2,...,n},构造网络G_1=\{V_1,E_...原创 2020-10-02 20:28:01 · 131 阅读 · 0 评论 -
luogu P1251 餐巾计划问题(网络流24题 费用流 + 拆点建图)
题目描述一个餐厅在相继的NN天里,每天需用的餐巾数不尽相同。假设第ii天需要r_iri块餐巾( i=1,2,...,N)。餐厅可以购买新的餐巾,每块餐巾的费用为pp分;或者把旧餐巾送到快洗部,洗一块需 m 天,其费用为 f 分;或者送到慢洗部,洗一块需nn天(n>mn>m),其费用为ss分(s<fs<f)。每天结束时,餐厅必须决定将多少块脏的餐巾送到快洗部,多少块餐巾送到慢洗部,以及多少块保存起来延期送洗。但是每天洗好的餐巾和购买的新餐巾数之和,要满足...原创 2020-10-02 11:42:54 · 180 阅读 · 0 评论 -
luogu P2754 [CTSC1999]家园 / 星际转移问题(网络流24题 分层图 + 网络流)
题目描述由于人类对自然资源的消耗,人们意识到大约在 2300 年之后,地球就不能再居住了。于是在月球上建立了新的绿地,以便在需要时移民。令人意想不到的是,2177 年冬由于未知的原因,地球环境发生了连锁崩溃,人类必须在最短的时间内迁往月球。现有nn个太空站位于地球与月球之间,且有mm艘公共交通太空船在其间来回穿梭。每个太空站可容纳无限多的人,而太空船的容量是有限的,第ii艘太空船只可容纳h_ihi个人。每艘太空船将周期性地停靠一系列的太空站,例如(1,3,4)(1,3,4)表示...原创 2020-09-26 22:05:08 · 274 阅读 · 0 评论 -
Gym - 102394E Exchanging Gifts (2019CCPC哈尔滨站 拓扑排序)
After the dress rehearsal of CCPC Harbin Site 2019,mmcontestants are still in the contest arena. They are taking photos, discussing the problems, and exchanging gifts.Initially, everyone has exactly one gift in their hand. Note that some contestants...原创 2020-09-24 16:38:24 · 532 阅读 · 0 评论 -
HDU - 6567 Cotree(树的重心 + 树形dp)
Avin has two trees which are not connected. He asks you to add an edge between them to make them connected while minimizing the function∑ni=1∑nj=i+1dis(i,j)∑i=1n∑j=i+1ndis(i,j), wheredis(i,j)dis(i,j)represents the number of edges of the path fromiito...原创 2020-09-24 14:56:54 · 370 阅读 · 0 评论 -
HDU - 6705 path(图上不固定起点和终点的第 k 短路)
You have a directed weighted graph withnnvertexes andmmedges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.Now there areqqqueries...原创 2020-09-17 16:39:49 · 251 阅读 · 0 评论 -
HDU - 4786 Fibonacci Tree(最小生成树 + 最大生成树)
Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem: Consider a bidirectional graph G with N vertices and M edges. All edges are painted into.原创 2020-09-15 20:32:56 · 257 阅读 · 0 评论 -
牛客练习赛69 C旅行(最大生成树)
链接:https://ac.nowcoder.com/acm/contest/7329/C来源:牛客网最小生成树表示的是 n 个点的路径代价之和最小,该题求 n 个点路径代价之和的最大值,也就是求最大生成树。证明:转自https://blog.youkuaiyun.com/weixin_43872728/article/details/108548041作者:scimoon可以发现,对答案有贡献的边肯定是最大生成树上的边,那么可以将这些边先拉出来,每条边至少会被贡献一次对于当前的一个联通块,找到原创 2020-09-15 19:13:52 · 193 阅读 · 0 评论 -
hdu2135 Farm Tour (最小费用最大流)
Farm TourTime Limit:1000MS Memory Limit:65536K Total Submissions:22944 Accepted:8792 DescriptionWhen FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1....原创 2020-09-04 20:25:16 · 158 阅读 · 0 评论 -
POJ - 2516 Minimum Cost (最小费用)
Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy has M supply places (marked from 1 to M), each provides K different kinds of g原创 2020-09-03 10:38:34 · 161 阅读 · 0 评论 -
POJ - 2195 Going Home (最小费用最大流模板 Bellman-Ford + spfa + dijkstra)
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he原创 2020-09-02 21:41:14 · 193 阅读 · 0 评论 -
POJ - 1087 A Plug for UNIX (网络流最大流 + 建图)
You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as...原创 2020-09-02 19:11:23 · 242 阅读 · 0 评论 -
POJ - 3281 Dining (网络流最大流 + 拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be a原创 2020-09-02 18:58:01 · 175 阅读 · 0 评论 -
poj3436 ACM Computer Factory(网络流最大流 + 拆点 + 建图 + 输出路径)
ACM Computer FactoryTime Limit:1000MS Memory Limit:65536K Total Submissions:12151 Accepted:4563 Special Judge DescriptionAs you know, all the computers used for ACM contests must be identical, so the participants compete o...原创 2020-09-02 11:39:33 · 239 阅读 · 0 评论 -
poj1149 PIGS(网络流最大流 + 建图)
PIGSTime Limit:1000MS Memory Limit:10000K Total Submissions:24778 Accepted:11281 DescriptionMirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Cust...原创 2020-08-31 21:55:33 · 293 阅读 · 0 评论 -
ACM-ICPC Nanjing Onsite 2018 Magic Potion(网络流最大流 + 建图)
传送门There arennheroes andmmmonsters living in an island. The monsters became very vicious these days, so the heroes decided to diminish the monsters in the island. However, theii-th hero can only kill one monster belonging to the setM_iMi. Joe, the...原创 2020-08-31 19:43:55 · 289 阅读 · 0 评论
分享