\(\text{Problem}\)

有向不联通图,求每个子图至多选出一条最大权值和的路径,求前 \(k+1\) 个

\(\text{Solution}\)

显然将每个子图缩点后 \(dp\),排序 \(dp\) 值即可

很多细节要处理

\(\text{Code}\)

#include<cstdio>
#include<algorithm>
#define re register
using namespace std; const int N = 1e5 + 5, M = 1e6 + 5;
int n, m, k, a[N], Q[N], len; struct edge{
int to, nxt;
}e[M], E[M], _e[M];
int h[N], H[N], _h[N]; inline void add(int x, int y)
{
static int tot = 0;
e[++tot] = edge{y, h[x]}, h[x] = tot;
}
inline void Add(int x, int y)
{
static int Tot = 0;
E[++Tot] = edge{y, H[x]}, H[x] = Tot;
}
inline void _add(int x, int y)
{
static int _tot = 0;
_e[++_tot] = edge{y, _h[x]}, _h[x] = _tot;
} int bz[N];
void find(int x)
{
Q[++len] = x, bz[x] = 1;
for(re int i = H[x]; i; i = E[i].nxt)
{
int v = E[i].to;
if (bz[v]) continue;
find(v);
}
} int dfn[N], low[N], vis[N], st[N], top, dfc, col[N], color;
void tarjan(int x)
{
st[++top] = x, dfn[x] = low[x] = ++dfc, vis[x] = 1;
for(re int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (!dfn[v]) tarjan(v), low[x] = min(low[x], low[v]);
else if (vis[v]) low[x] = min(low[x], dfn[v]);
}
if (low[x] == dfn[x])
{
col[x] = ++color, vis[x] = 0;
while (st[top] != x) col[st[top]] = color, vis[st[top]] = 0, --top;
--top;
}
} int _Q[N], f[N], g[N], cnt, in[N], _in[N], cur, val[N];
inline void topu()
{
for(re int i = 1; i <= len; i++)
{
val[col[Q[i]]] += a[Q[i]];
for(re int j = h[Q[i]]; j; j = e[j].nxt)
if (col[e[j].to] != col[Q[i]]) _add(col[Q[i]], col[e[j].to]);
}
for(re int i = cur + 1; i <= color; i++)
for(re int j = _h[i]; j; j = _e[j].nxt) _in[_e[j].to]++; int head = 0, tail = 0;
for(re int i = cur + 1; i <= color; i++)
{
f[i] = val[i];
if (!_in[i]) _Q[++tail] = i;
}
while (head < tail)
{
int now = _Q[++head];
for(re int i = _h[now]; i; i = _e[i].nxt)
{
f[_e[i].to] = max(f[_e[i].to], f[now] + val[_e[i].to]), --_in[_e[i].to];
if (!_in[_e[i].to]) _Q[++tail] = _e[i].to;
}
}
++cnt;
for(re int i = cur + 1; i <= color; i++) g[cnt] = max(g[cnt], f[i]);
cur = color;
} int main()
{
freopen("azeroth.in", "r", stdin);
freopen("azeroth.out", "w", stdout);
scanf("%d%d", &n, &m);
for(re int i = 1, u, v; i <= m; i++)
{
scanf("%d%d", &u, &v);
if (u ^ v) add(u, v), Add(u, v), Add(v, u);
}
for(re int i = 1; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &k);
for(re int i = 1; i <= n; i++)
if (!bz[i])
{
len = top = 0, find(i);
for(re int j = 1; j <= len; j++)
if (!dfn[Q[j]]) tarjan(Q[j]);
topu();
}
sort(g + 1, g + cnt + 1), ++k;
int ans = 0;
for(re int i = cnt; i && k; --i, --k) ans += g[i];
printf("%d\n", ans);
}

JZOJ 4253.QYQ在艾泽拉斯的更多相关文章

  1. 一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(四) --高级设置二

    原文:一个完整的Installshield安装程序实例-艾泽拉斯之海洋女神出品(四) --高级设置二 上一篇:一个完整的安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一4. 根据用户选择的组 ...

  2. 关于Installshield里一些常见问题的解答—艾泽拉斯之海洋女神出品

    原文:关于Installshield里一些常见问题的解答-艾泽拉斯之海洋女神出品 上一篇:一个完整的安装程序实例—艾泽拉斯之海洋女神出品(五) --补遗转载时请务必保留转载出处和由艾泽拉斯之海洋女神出 ...

  3. (jzoj snow的追寻)线段树维护树的直径

    jzoj snow的追寻 DFS序上搞 合并暴力和,记录最长链和当前最远点,距离跑LCA # include <stdio.h> # include <stdlib.h> # ...

  4. [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)

    Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...

  5. [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)

    Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...

  6. [jzoj]5478.【NOIP2017提高组正式赛】列队

    Link https://jzoj.net/senior/#main/show/5478 Description Sylvia 是一个热爱学习的女孩子.       前段时间,Sylvia 参加了学校 ...

  7. [jzoj]1115.【HNOI2008】GT考试

    Link https://jzoj.net/senior/#main/show/1115 Description 申准备报名参加GT考试,准考证号为n位数X1X2X3...Xn-1Xn(0<=X ...

  8. [jzoj]2538.【NOIP2009TG】Hankson 的趣味题

    Link https://jzoj.net/senior/#main/show/2538 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫H ...

  9. [jzoj]4216.【NOIP2015模拟9.12】平方和

    Link https://jzoj.net/senior/#main/show/4216 Description 给出一个N个整数构成的序列,有M次操作,每次操作有一下三种: ①Insert Y X, ...

  10. [jzoj]2938.【NOIP2012模拟8.9】分割田地

    Link https://jzoj.net/senior/#main/show/2938 Description 地主某君有一块由2×n个栅格组成的土地,有k个儿子,现在地主快要终老了,要把这些土地分 ...

随机推荐

  1. 【Spark】Day06-Spark高级课程:性能调优、算子调优、Shuffle调优、JVM调优、数据倾斜、TroubleShooting

    一.Spark性能调优 1.常规性能调优 (1)最优资源配置:Executor数量.Executor内存大小.CPU核心数量&Driver内存 (2)RDD优化:RDD复用.RDD持久化(序列 ...

  2. Android ViewPager2 + TabLayout + BottomNavigationView

    Android ViewPager2 + TabLayout + BottomNavigationView 实际案例 本篇主要介绍一下 ViewPager2 + TabLayout + BottomN ...

  3. Hive详解(04) - hive函数的使用

    Hive详解(04) - hive函数的使用 系统内置函数 查看系统自带的函数 hive> show functions; 显示自带的函数的用法 hive> desc function u ...

  4. mysql基础命令语法

    删除空格 update 表名 set 字段名 = replace(字段名 ,' ','') ; 临时表创建与删除 -- 创建临时表 create temporary table if not exis ...

  5. python之路30 网络编程之初识并发编程1

    并发编程理论 研究网络编程其实就是在研究计算机的底层原理及发展史 """ 计算机中真正干活的是CPU """ 操作系统发展史 1.穿孔卡片阶 ...

  6. python进阶之路10之函数

    函数前戏 name_list = ['jason', 'kevin', 'oscar', 'jerry'] # print(len(name_list)) '''突然len不准用了''' # coun ...

  7. 一、tcp三次握手

    (1)首先客户端向服务器端发送一段tcp报文,其中标志位为SYN,表示"请求创建新连接"序列号为sqe=x,随后进入SYN_SEND状态 (2)服务器端接受到客户端发送的tcp报文 ...

  8. Hugging Face 2023 实习生招募计划

    Hugging Face 2023 实习生招募计划 想参与到 <王婆卖瓜>「最酷的 AI 社区」</王婆卖瓜>,共同构建未来吗?今天,我们为大家分享 Hugging Face ...

  9. 推荐一款在浏览器编辑`Blazor`的`IDE`

    不知道是否有Blazor用户羡慕过React或者Vue用户,在一些组件库中,它们就提供了在当前的组件预览对于组件的实时编辑并且预览? 比如semi-design的这种 在比如codepen这种 由于B ...

  10. 今天学到的新知识--使用localtunnel实现内网穿透,感觉很神奇哇~~

    localtunnel 是一个基于 nodejs 的内网穿透工具.通过简单的安装可以实现将内网里的设备的某个端口暴露在公网中以提供服务. 首先你电脑要有node环境 使用本地隧道,对应本地服务的端口号 ...