Luogu P43916 图的遍历
我们把“u点能够到达的最大点”转化为反向图中能到达u点的所有点里的最大值,可知缩点后满足无后效性。val[i]的初值设为连通分量i中的最大点。反向存图,tarjan缩点,拓扑序dp即可。
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #define maxn 100100
- using namespace std;
- int n, m;
- void read(int &x) {
- x = 0;
- char ch = getchar();
- while (!isdigit(ch))
- ch = getchar();
- while (isdigit(ch))
- x = x * 10 + (ch ^ 48),
- ch = getchar();
- return;
- }
- struct E {
- int to, nxt;
- } edge[maxn], edge2[maxn];
- int head[maxn], top, head2[maxn], top2;
- inline void insert(int u, int v) {
- edge[++top] = (E) {v, head[u]};
- head[u] = top;
- }
- inline void insert2(int u, int v) {
- edge2[++top2] = (E) {v, head2[u]};
- head2[u] = top2;
- }
- int low[maxn], dfn[maxn], timer, c[maxn], cnt, sta[maxn], stp;
- bool ins[maxn];
- int val[maxn];
- void tarjan(int u) {
- low[u] = dfn[u] = ++ timer;
- sta[++stp] = u, ins[u] = true;
- for (int i = head[u]; i; i = edge[i].nxt) {
- int v = edge[i].to;
- if (!dfn[v]) {
- tarjan(v);
- low[u] = min(low[u], low[v]);
- } else if (ins[v])
- low[u] = min(low[u], dfn[v]);
- }
- if (dfn[u] == low[u]) {
- ++cnt;
- int x;
- do {
- x = sta[stp--];
- ins[x] = false;
- c[x] = cnt;
- val[cnt] = max(val[cnt], x);
- } while (x != u);
- }
- }
- int ind[maxn];
- void build() {
- for (int u = 1; u <= n; ++u)
- for (int i = head[u]; i; i = edge[i].nxt) {
- int v = edge[i].to;
- if (c[u] != c[v])
- insert2(c[u], c[v]), ++ind[c[v]];
- }
- }
- void dp() {
- queue<int> que;
- for (int i = 1; i <= cnt; ++i)
- if (!ind[i]) que.push(i);
- while (!que.empty()) {
- int u = que.front(); que.pop();
- for (int i = head2[u]; i; i = edge2[i].nxt) {
- int v = edge2[i].to;
- val[v] = max(val[v], val[u]);
- --ind[v];
- if (!ind[v])
- que.push(v);
- }
- }
- return;
- }
- int main() {
- read(n), read(m);
- int u, v;
- for (int i = 1; i <= m; ++i) {
- read(u), read(v);
- insert(v, u);
- }
- for (int i = 1; i <= n; ++i)
- if (!dfn[i])
- tarjan(i);
- build();
- dp();
- for (int i = 1; i <= n; ++i)
- printf("%d ", val[c[i]]);
- return 0;
- }
至此luogu上真哥留下的缩点习题全部完成。晚上更新对最小树形图(朱刘算法)的理解。
Luogu P43916 图的遍历的更多相关文章
- luogu P3916 图的遍历
P3916 图的遍历 题目描述 给出 N 个点, M 条边的有向图,对于每个点 v ,求 A(v) 表示从点 v 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 N,MN ...
- Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925
说明 • 对于60% 的数据, n,m在1e3内 • 对于100% 的数据, n,m在1e5内. 本弱弱上来就是一顿暴搜打,dfs n次,每次更新答案,复杂度为O(n*n),果然TLE,60分抱回家. ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)
图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...
- Kruskal和prime算法的类实现,图的遍历BFS算法。
一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- 图的遍历——DFS和BFS模板(一般的图)
关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...
- 图的遍历算法:DFS、BFS
在图的基本算法中,最初需要接触的就是图的遍历算法,根据访问节点的顺序,可分为深度优先搜索(DFS)和广度优先搜索(BFS). DFS(深度优先搜索)算法 Depth-First-Search 深度优先 ...
- 15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法
算法分析和具体步骤解说直接写在代码注释上了 TvT 没时间了等下还要去洗衣服 就先不赘述了 有不明白的欢迎留言交流!(估计是没人看的了) 直接上代码: #include<stdio.h> ...
随机推荐
- 6个冷门但实用的pandas知识点
1 简介 pandas作为开展数据分析的利器,蕴含了与数据处理相关的丰富多样的API,使得我们可以灵活方便地对数据进行各种加工,但很多pandas中的实用方法其实大部分人都是不知道的,今天就来给大家介 ...
- 细说React生命周期
目录 新旧版本生命周期图对比 16.3之前的版本 16.3之后的版本 生命周期的几个阶段 挂载 constructor conpomentWillMount(v17将移除) getDerivedSta ...
- requests 库和beautifulsoup库
python 爬虫和解析 库的安装:pip install requests; pip install beautifulsoup4 requests 的几个常用方法: requests.reques ...
- 基于 opencv 的图像处理入门教程
前言 虽然计算机视觉领域目前基本是以深度学习算法为主,但实际上很多时候对图片的很多处理方法,并不需要采用深度学习的网络模型,采用目前成熟的图像处理库即可实现,比如 OpenCV 和 PIL ,对图片进 ...
- STM32入门系列-GPIO概念介绍
GPIO(general purpose intput output)是通用输入输出端口的简称,可以通过软件来控制其输入和输出.STM32 芯片的 GPIO 引脚与外部设备连接起来,从而实现与外部通讯 ...
- RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
问题 在用pytorch跑生成对抗网络的时候,出现错误Runtime Error: one of the variables needed for gradient computation has b ...
- 洛谷 P2391 白雪皑皑 线段树+优化
题目描述: 现在有 \(N\) 片雪花排成一列. Pty 要对雪花进行$ M $次染色操作,第 \(i\)次染色操作中,把\((i*p+q)%N+1\) 片雪花和第\((i*q+p)%N+1\)片雪花 ...
- c++11-17 模板核心知识(一)—— 函数模板
1.1 定义函数模板 1.2 使用函数模板 1.3 两阶段翻译 Two-Phase Translation 1.3.1 模板的编译和链接问题 1.4 多模板参数 1.4.1 引入额外模板参数作为返回值 ...
- maven 搭建私服
博客参考 https://www.cnblogs.com/luotaoyeah/p/3791966.html 整理纯为技术学习 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程 ...
- 【Kata Daily 190916】String end with?(字母结尾)
题目: Complete the solution so that it returns true if the first argument(string) passed in ends with ...