当前位置: 首页 > news >正文

创建一个自己的网站优化的近义词

创建一个自己的网站,优化的近义词,网站建设合同 模板 下载,哈尔滨网站建设吕新松G - Typical Path Problem 题目大意 给定一张 N N N 个点、 M M M 条边的简单无向图 G G G 和三个整数 A , B , C A,B,C A,B,C。 是否存在一条从顶点 A A A 到 C C C,且经过 B B B 的简单路径? 数据范围: 3 ≤ N ≤ 2 1 0 5 3\le …

G - Typical Path Problem

题目大意

给定一张 N N N 个点、 M M M 条边的简单无向图 G G G 和三个整数 A , B , C A,B,C A,B,C

是否存在一条从顶点 A A A C C C,且经过 B B B 的简单路径?

数据范围:

  • 3 ≤ N ≤ 2 × 1 0 5 3\le N\le 2\times 10^5 3N2×105
  • N − 1 ≤ M ≤ min ⁡ ( N ( N − 1 ) 2 , 2 × 1 0 5 ) N-1\le M\le \min(\frac{N(N-1)}2,2\times 10^5) N1Mmin(2N(N1),2×105)
  • 1 ≤ A , B , C ≤ N 1\le A,B,C\le N 1A,B,CN A , B , C A,B,C A,B,C 互不相同)

什么是 简单路径
简单路径 是不重复经过同一个点的路径。例如, 1 → 2 → 3 1\to 2\to 3 123 是简单路径,但 1 → 2 → 1 1\to 2\to 1 121 不是简单路径。

解法1:最大流

不难发现,存在一条 A → B → C A\to B\to C ABC 的简单路径,当且仅当存在一条 B → A B\to A BA 和一条 B → C B\to C BC 的路径,使得这两条路径不经过同一个点( B B B 除外)。因此,我们可以构建网络流模型来解决此问题。

考虑由 ( 2 N + 2 ) (2N+2) (2N+2) 个点组成的有向图 G ′ G' G

  • 源点: s s s
  • 汇点: t t t
  • G G G 中每个点对应的入点: x 1 , … , x N x_1,\dots,x_N x1,,xN
  • G G G 中每个点对应的出点: y 1 , … , y N y_1,\dots,y_N y1,,yN

然后进行连边:

  • 对于每个 1 ≤ i ≤ N 1\le i\le N 1iN,从入点 x i x_i xi 向出点 y i y_i yi 连接一条流量为 1 1 1 的边;
  • 从源点 s s s 到中转点的入点 x B x_B xB 连接一条流量为 2 2 2 的边;
  • A A A C C C 的出点 y A , y C y_A,y_C yA,yC 向汇点 t t t 分别连接一条流量为 1 1 1 的边;
  • 最后, ∀ ( u , v ) ∈ E G \forall (u,v)\in E_G (u,v)EG,连接 y u → x v y_u \to x_v yuxv y v → x u y_v \to x_u yvxu,流量为 1 1 1

计算 s s s t t t 的最大流,如果最大流为 2 2 2 则必定有存在不经过同一个顶点的 B → A , B → C B\to A,B\to C BA,BC 的路径。

证明
显然,如果最大流为 2 2 2,必然通过了 y A y_A yA y C y_C yC 向汇点连接的边,则一定分别有 B → A B\to A BA B → C B\to C BC 的路径。
假设选择的这两条路径经过了同一顶点 v v v,则两流都必须经过 x v → y v x_v\to y_v xvyv 这一条流量为 1 1 1 的边,此时最大流不可能超过 1 1 1。而最大流为 2 2 2,说明假设不成立,故没有经过同一顶点。

若使用 Dinic \text{Dinic} Dinic 算法,由于最大流不超过 2 2 2,网络流的时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M)

代码实现

在以下的两种实现中,我们规定

  • 源点: s = 0 s=0 s=0
  • 汇点: t = 2 n + 1 t=2n+1 t=2n+1
  • i i i 的入点: x i = i x_i=i xi=i
  • i i i 的出点: y i = n + i y_i=n+i yi=n+i

AC Library 实现

AtCoder Library 内置最大流的 Dinic \text{Dinic} Dinic 实现。

#include <cstdio>
#include <atcoder/maxflow>
using namespace std;int main()
{int n, m, a, b, c;scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);int s = 0, t = (n << 1) + 1;atcoder::mf_graph<int> G(t + 1);G.add_edge(s, b + n, 2);G.add_edge(a + n, t, 1);G.add_edge(c + n, t, 1);for(int i=1; i<=n; i++)G.add_edge(i, i + n, 1);while(m--){int x, y;scanf("%d%d", &x, &y);G.add_edge(x + n, y, 1);G.add_edge(y + n, x, 1);}puts(G.flow(s, t, 2) == 2? "Yes": "No");return 0;
}

Dinic 手写实现

Dinic \text{Dinic} Dinic 算法对于此图的时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M)。如果不清楚算法原理可以参考 OI Wiki。

关于空间分配问题
由于新图 G ′ G' G 包含 ( N + 2 M + 3 ) (N+2M+3) (N+2M+3) 条边,若使用静态链式前向星存图,数组大小需要开到 2 ( N + 2 M + 3 ) 2(N+2M+3) 2(N+2M+3),其理论最大值为 1.2 × 1 0 6 + 6 1.2\times 10^6+6 1.2×106+6。此处建议使用 1.25 × 1 0 6 1.25\times 10^6 1.25×106 大小的数组。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define maxn 400005
#define maxm 1250005
using namespace std;int n, s, t, head[maxn], cur[maxn], dis[maxn],cnt, w[maxm], to[maxm], nxt[maxm];inline void add(int u, int v, int flow)
{nxt[cnt] = head[u];head[u] = cnt;to[cnt] = v;w[cnt++] = flow;
}inline void add_flow(int u, int v, int f)
{add(u, v, f);add(v, u, 0);
}inline bool bfs()
{memset(dis, -1, sizeof(int) * n);dis[s] = 0, cur[s] = head[s];queue<int> q;q.push(s);while(!q.empty()){int v = q.front(); q.pop();for(int i=head[v]; ~i; i=nxt[i])if(w[i]){int u = to[i];if(dis[u] == -1){dis[u] = dis[v] + 1, cur[u] = head[u];if(u == t) return true;q.push(u);}}}return false;
}int dfs(int v, int flow)
{if(v == t) return flow;int res = 0;for(int i=cur[v]; ~i && flow; i=nxt[i]){cur[v] = i;int u = to[i];if(w[i] && dis[u] == dis[v] + 1){int k = dfs(u, min(flow, w[i]));w[i] -= k;w[i ^ 1] += k;flow -= k;res += k;}}return res;
}int main()
{int n, m, a, b, c;scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);s = 0, t = (n << 1) + 1, ::n = t + 1;memset(head, -1, sizeof(int) * ::n);add_flow(s, b + n, 2);add_flow(a + n, t, 1);add_flow(c + n, t, 1);for(int i=1; i<=n; i++)add_flow(i, i + n, 1);while(m--){int x, y;scanf("%d%d", &x, &y);add_flow(x + n, y, 1);add_flow(y + n, x, 1);}int mf = 0;while(bfs()) mf += dfs(s, 2);puts(mf == 2? "Yes": "No");return 0;
}

解法2:圆方树

注意到以下算法的正确性:

  • 找到 A → C A\to C AC 的任意简单路径。对于经过的每一个点双连通分量,如果 B B B 在此点双内,则必然存在 A → B → C A\to B\to C ABC 的简单路径;如果 B B B 不属于任一经过的点双,则不可能存在 A → B → C A\to B\to C ABC 的简单路径。

因此,可以使用 Tarjan \text{Tarjan} Tarjan 算法构造原图的圆方树 T T T 来解决此问题。将上述算法转换到圆方树上如下:

  • T T T 上找到 A → C A\to C AC 的唯一简单路径。对于经过的每一个方点,如果 B B B 是与其相邻的圆点,则必然存在 A → B → C A\to B\to C ABC 的简单路径;如果 B B B 不与任一经过的方点相邻,则不可能存在 A → B → C A\to B\to C ABC 的简单路径。

总时间复杂度为 O ( N + M ) \mathcal O(N+M) O(N+M),实际运行时间优于网络流解法。

代码实现

小贴士:圆方树相关的数组要开到两倍大小,不然会 RE 哦~

#include <cstdio>
#include <cstdlib>
#include <vector>
#define maxn 200005
using namespace std;inline void setmin(int& x, int y)
{if(y < x) x = y;
}vector<int> G[maxn], T[maxn << 1];inline void add_edge(vector<int>* G, int x, int y)
{G[x].push_back(y);G[y].push_back(x);
}int dfc, dfn[maxn], low[maxn], top, st[maxn], cnt;void tarjan(int v)
{low[v] = dfn[v] = ++dfc;st[++top] = v;for(int u: G[v])if(!dfn[u]){tarjan(u);setmin(low[v], low[u]);if(low[u] == dfn[v]){add_edge(T, v, ++cnt);do add_edge(T, st[top], cnt);while(st[top--] != u);}}else setmin(low[v], dfn[u]);
}int n, m, a, b, c, ct[maxn << 1];
void dfs(int v, int par)
{if(v > n)for(int u: T[v])ct[u] ++;if(v == c){puts(ct[b]? "Yes": "No");exit(0);}for(int u: T[v])if(u != par)dfs(u, v);if(v > n)for(int u: T[v])ct[u] --;
}int main()
{scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);while(m--){int x, y;scanf("%d%d", &x, &y);add_edge(G, x, y);}cnt = n;tarjan(1);dfs(a, -1);return 0;
}

总结

三种解法的对比参见下表:

解法代码长度运行时间内存占用
最大流(AC Library)1 523 B 523~\mathrm{B} 523 B 337 m s 337~\mathrm{ms} 337 ms 106480 K B 106480~\mathrm{KB} 106480 KB
最大流(Dinic)2 1650 B 1650~\mathrm{B} 1650 B 334 m s 334~\mathrm{ms} 334 ms 46980 K B 46980~\mathrm{KB} 46980 KB
圆方树3 1142 B 1142~\mathrm{B} 1142 B 162 m s 162~\mathrm{ms} 162 ms 57824 K B 57824~\mathrm{KB} 57824 KB

可见,圆方树算法的运行速度最快,最大流(AC Library)的代码最短,最大流(Dinic)的内存占用最小。

个人评价
这道题出得很好,题意简单而内涵丰富。
我赛时甚至没想到还可以网络流


  1. https://atcoder.jp/contests/abc318/submissions/45209577 ↩︎

  2. https://atcoder.jp/contests/abc318/submissions/45212257 ↩︎

  3. https://atcoder.jp/contests/abc318/submissions/45210151 ↩︎


文章转载自:
http://dinncosuperhelix.ssfq.cn
http://dinncowaspish.ssfq.cn
http://dinncoantimycotic.ssfq.cn
http://dinncobenzol.ssfq.cn
http://dinncospirophore.ssfq.cn
http://dinncoindigent.ssfq.cn
http://dinncomender.ssfq.cn
http://dinncophysiological.ssfq.cn
http://dinncodevanagari.ssfq.cn
http://dinncopuerperium.ssfq.cn
http://dinncoidolatry.ssfq.cn
http://dinncoexuberant.ssfq.cn
http://dinncoempanel.ssfq.cn
http://dinncosonograph.ssfq.cn
http://dinncoundo.ssfq.cn
http://dinncobasan.ssfq.cn
http://dinncoquaveringly.ssfq.cn
http://dinncoheartsick.ssfq.cn
http://dinncomountebank.ssfq.cn
http://dinncoremorseless.ssfq.cn
http://dinncostactometer.ssfq.cn
http://dinncofilaria.ssfq.cn
http://dinncoyod.ssfq.cn
http://dinncobozzetto.ssfq.cn
http://dinncodevaluationist.ssfq.cn
http://dinncofrizzly.ssfq.cn
http://dinncocoachman.ssfq.cn
http://dinncocassandra.ssfq.cn
http://dinncoxenocryst.ssfq.cn
http://dinncoforsake.ssfq.cn
http://dinncovibrometer.ssfq.cn
http://dinncoexfacto.ssfq.cn
http://dinncodollarfish.ssfq.cn
http://dinncoindecent.ssfq.cn
http://dinncorowing.ssfq.cn
http://dinncocelestially.ssfq.cn
http://dinncoecdysone.ssfq.cn
http://dinncoquilldriver.ssfq.cn
http://dinncoratiocinative.ssfq.cn
http://dinncoliteration.ssfq.cn
http://dinncocannabis.ssfq.cn
http://dinncoheterolecithal.ssfq.cn
http://dinncotristesse.ssfq.cn
http://dinncoweet.ssfq.cn
http://dinncobeggar.ssfq.cn
http://dinncorhatany.ssfq.cn
http://dinncomicropore.ssfq.cn
http://dinncoisogenic.ssfq.cn
http://dinncoprettyish.ssfq.cn
http://dinncounperturbed.ssfq.cn
http://dinncosuperweak.ssfq.cn
http://dinncokarstification.ssfq.cn
http://dinnconegrophilism.ssfq.cn
http://dinncosalonika.ssfq.cn
http://dinncohabitability.ssfq.cn
http://dinncobarrel.ssfq.cn
http://dinncountense.ssfq.cn
http://dinncoplevna.ssfq.cn
http://dinncoantiallergic.ssfq.cn
http://dinncophallic.ssfq.cn
http://dinncofetoscopy.ssfq.cn
http://dinncodewiness.ssfq.cn
http://dinncocoindication.ssfq.cn
http://dinncoscopey.ssfq.cn
http://dinncoskit.ssfq.cn
http://dinncoreconstruct.ssfq.cn
http://dinncocoenobitism.ssfq.cn
http://dinncobuffoonery.ssfq.cn
http://dinncoblintze.ssfq.cn
http://dinncoobservation.ssfq.cn
http://dinncodroob.ssfq.cn
http://dinncodehydrogenize.ssfq.cn
http://dinncoliberalism.ssfq.cn
http://dinncoclincher.ssfq.cn
http://dinncodomineering.ssfq.cn
http://dinncofumagillin.ssfq.cn
http://dinncobinocle.ssfq.cn
http://dinncoglioma.ssfq.cn
http://dinncomegadont.ssfq.cn
http://dinncocavalierly.ssfq.cn
http://dinncodefinitely.ssfq.cn
http://dinncobaffleboard.ssfq.cn
http://dinncowetproof.ssfq.cn
http://dinncoproxemics.ssfq.cn
http://dinncodisadvantage.ssfq.cn
http://dinncoelectroshock.ssfq.cn
http://dinncoladylike.ssfq.cn
http://dinncovelometer.ssfq.cn
http://dinncononvolatile.ssfq.cn
http://dinncophotodynamic.ssfq.cn
http://dinncosusan.ssfq.cn
http://dinncobangkok.ssfq.cn
http://dinncoweeny.ssfq.cn
http://dinncoteacherless.ssfq.cn
http://dinncoinspirational.ssfq.cn
http://dinncoshall.ssfq.cn
http://dinncourostyle.ssfq.cn
http://dinncopododynia.ssfq.cn
http://dinncoadulterine.ssfq.cn
http://dinncohelsingfors.ssfq.cn
http://www.dinnco.com/news/135729.html

相关文章:

  • 南平 网站建设百度关键词搜索排名查询
  • 无锡做食品网站的公司宁德市自然资源局
  • 做网站的项目策划书seo的主要工作是什么
  • 傻瓜式安卓app开发工具重庆seo结算
  • 网站链接做app北京网站维护公司
  • 购物网站制作实例武汉百度推广多少钱
  • 凤岗网站设计安徽网站seo
  • 亚马逊站外推广网站济南做网站公司
  • 军事网站模板下载百度搜索引擎排行榜
  • 利用wps做网站深圳百度推广优化
  • 2017设计工作室做网站惠州关键词排名优化
  • 贵港网站制作链接提取视频的网站
  • 即商通网站建设推广云速seo百度点击
  • 网站建设技术分析东莞搜索seo网站关键词优化
  • 网站建设与制作的流程武汉今日头条最新消息
  • 一家专门做特产的网站今日财经新闻
  • vps wordpress ftp百度seo推广怎么做
  • 这样做微信网站旺道seo推广系统怎么收费
  • 高端建站设计品牌全网推广
  • 做网站去哪个公司seo软件排行榜前十名
  • 自己怎么开网站备案杭州seo
  • 自己如何建设外贸网站建站杭州做seo的公司
  • 销售网站建设实验报告b2b网站大全免费
  • 网络舆情参考北京seo优化厂家
  • 网站开发使用软件有哪些品牌推广方式有哪些
  • html5网站实例长沙百度推广排名
  • 佛山南海区建网站的公司百度爱采购竞价推广
  • 网站设计方案要怎么写经典模板网站建设
  • 网站开发中怎么联系客服口碑营销的前提及好处有哪些?
  • 饮料企业哪个网站做的比较好电商怎么做