题目

好久没法博客了

这次就水个板子题目吧

tarjan缩点之后重新建图

而且边权应该都是正的(要不我怎么能这么轻松水过去)

在新图上记忆化一下就好了

f[i] 表示 开头选i这个点 的 路径最大值

#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;
int n,m,w[maxn],f[maxn];
int stak[maxn],top,vis[maxn],dfn[maxn],low[maxn],cnt;//一套trajan
int color[maxn],color_tot,color_w[maxn];//一套染色数组
struct edge {
int u,v,nxt;
}old_e[maxn],new_e[maxn];
int old_head[maxn],old_tot;
int new_head[maxn],new_tot;
void add1(int u,int v) {
old_e[++old_tot].u=u;
old_e[old_tot].v=v;
old_e[old_tot].nxt=old_head[u];
old_head[u]=old_tot;
}
void add2(int u,int v) {
new_e[++new_tot].v=v;
new_e[new_tot].nxt=new_head[u];
new_head[u]=new_tot;
}
void tarjan(int u) {
dfn[u]=low[u]=++cnt;
vis[u]=1;
stak[++top]=u;
for(int i=old_head[u];i;i=old_e[i].nxt) {
int v=old_e[i].v;
if(!dfn[v]) {
tarjan(v);
dfn[u]=min(dfn[u],dfn[v]);
} else
if(vis[v])
dfn[u]=min(dfn[u],low[v]);
}
if(dfn[u]==low[u]) {
++color_tot;
while(stak[top]!=u) {
vis[stak[top]]=0;
color_w[color_tot]+=w[stak[top]];
color[stak[top--]]=color_tot;
}
top--;
color[u]=color_tot;
color_w[color_tot]+=w[u];
vis[u]=0;
}
}
//f[i] 表示 开头选i这个点的路径最大值
int dfs(int u)
{
if(f[u]) return f[u];
int tmp=0;
for(int i=new_head[u];i;i=new_e[i].nxt) {
int v=new_e[i].v;
tmp=max(tmp,dfs(v));
}
if(!tmp) return f[u]=color_w[u];
return f[u]=color_w[u]+tmp;
}
int main()
{
//freopen("testdata.in","r",stdin);
//读入
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&w[i]);
for(int i=1;i<=m;++i) {
int u,v;
scanf("%d%d",&u,&v);
add1(u,v);
}
//缩点
for(int i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
//建新图
for(int i=1;i<=old_tot;++i)
if(color[old_e[i].u]!=color[old_e[i].v])
{
add2(color[old_e[i].u],color[old_e[i].v]);
//cout<<color[old_e[i].u]<<" -> "<<color[old_e[i].v]<<"\n";
}
//记忆化搜索
int ans=0;
for(int i=1;i<=color_tot;++i)
ans=max(ans,dfs(i));
//输出ans
printf("%d\n",ans);
return 0;
}

luogu P3387 【模板】缩点的更多相关文章

  1. 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)

    Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...

  2. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  3. Luogu P2742 模板-二维凸包

    Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...

  4. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

  5. 解题报告+板子:luogu P3387 【模板】缩点

    题目链接:P3387 [模板]缩点 缩点板子,所谓\(dp\)就是拓扑排序(毕竟可以重走边),像\(SPFA\)一样松弛就好,就是重边极其烦人,还加了排序(绝对自己想的,然鹅拓扑的思路不是). 下面上 ...

  6. LUOGU P3387 【模板】缩点 (缩点+DAG dp)

    解题思路 缩点后按拓扑排序跑一个dp. #include<iostream> #include<cstdio> #include<cstring> #include ...

  7. luogu P3387 【模板】缩点_拓扑排序

    还是很好些的. Code: #include <stack> #include <cstdio> #include <algorithm> #include < ...

  8. Tarjan+topsort(DP)【P3387】 [模板]缩点

    Description 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...

  9. [模板](luogu P3387)縮點

    前言:對於這週的咕咕咕表示好像沒什麼好表示的,完全沒有靈感a......寫東西真的好難啊......於是又玩了半天鬼泣4???還挺好玩的 來源:題解 题目背景 缩点+DP 题目描述 给定一个n个点m条 ...

随机推荐

  1. HashMap实现原理分析(面试问题:两个hashcode相同 的对象怎么存入hashmap的)

    1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...

  2. Python 字典 dict() 函数

    描述 Python 字典 dict() 函数用于创建一个新的字典,用法与 Pyhon 字典 update() 方法相似. 语法 dict() 函数函数语法: dict(key/value) 参数说明: ...

  3. ORACLE 根据根节点查所有上层节点

    1.基本数据 SELECT * FROM TABLE_MUEN T ID         CODE                                           NAME     ...

  4. 2018/03/09 每日一个Linux命令 之 chgrp/chown

    每日一个Linux命令 2018-03-09 Linux 命令 chgrp/chown chgrp [-参数] [文件或者目录] chown [-参数] [文件所有者]:[文件所属群组] [文件或者目 ...

  5. AWTK 全称为 Toolkit AnyWhere,是 ZLG 倾心打造的一套基于 C 语言开发的 GUI 框架(三平台+2个手机平台+嵌入式)

    最终目标: 支持开发嵌入式软件. 支持开发Linux应用程序. 支持开发MacOS应用程序. 支持开发Windows应用程序. 支持开发Android应用程序. 支持开发iOS应用程序. 支持开发2D ...

  6. LightOj 1030 - Discovering Gold(dp+数学期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...

  7. 冒泡排序快速版(C)

    冒泡排序C语言版:在每轮排序中检查时候有元素位置交换,如果无交换,说明数组元素已经有序,无需继续排序 #include <stdio.h> #include <stdlib.h> ...

  8. python 使用set对列表去重,并保持列表原来顺序

    # python 使用set对列表去重,并保持列表原来顺序 list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] for item i ...

  9. poj2063 Investment

    http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...

  10. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...