cf 1027D Mouse Hunt

Codeforces 1027D Mouse Hunt【Tarjan缩点】 考虑答案的计算,由于老鼠会不断移动,而它最后一定会移动到一个没有其他房间可以移动的房间,所以我们就在出度为000的房间放陷阱即可。 但是如果老鼠最后移动到了一个环形房间的话,我们就只需要在这个环形房间放置陷阱代价最低的房间放陷阱就可以了。 所以我们只需要先缩点,然后统计出每个强连通分量的最小代价与出度,最后答案就是所有出度为0的最小代价之后。 #include <bits/stdc... 阅读详情

原题

D. Mouse Hunt

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%80% of applicants are girls and majority of them are going to live in the university dormitory for the next 44(hopefully) years.

The dormitory consists of nn rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number ii costs cici burles. Rooms are numbered from 11 to nn.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room ii in second tt then it will run to room aiai in second t+1t+1 without visiting any other rooms inbetween (i=aii=ai means that mouse won't leave room ii). It's second 00 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 11 to nn at second 00.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

Input

The first line contains as single integers nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of rooms in the dormitory.

The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1041≤ci≤104) — cici is the cost of setting the trap in room number ii.

The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — aiai is the room the mouse will run to the next second after being in room ii.

Output

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

Examples

input

Copy

5
1 2 3 2 10
1 3 4 3 3

output

Copy

3

input

Copy

4
1 10 2 10
2 4 2 2

output

Copy

10

input

Copy

7
1 1 1 1 1 1 1
2 2 2 3 6 7 6

output

Copy

2

Note

In the first example it is enough to set mouse trap in rooms 11 and 44. If mouse starts in room 11 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 44.

In the second example it is enough to set mouse trap in room 22. If mouse starts in room 22 then it gets caught immideately. If mouse starts in any other room then it runs to room 22 in second 11.

Here are the paths of the mouse from different starts from the third example:

  • 1→2→2→…1→2→2→…;
  • 2→2→…2→2→…;
  • 3→2→2→…3→2→2→…;
  • 4→3→2→2→…4→3→2→2→…;
  • 5→6→7→6→…5→6→7→6→…;
  • 6→7→6→…6→7→6→…;
  • 7→6→7→…7→6→7→…;

So it's enough to set traps in rooms 22 and 66.

 

分析

给你n个点,每个点有一个权重和唯一的后继。你要从中选择一个权重和最小的点集S,使得从任何点出发,最后都会到达S中的某个点。

n个点,每个点出度为1。这样的图中一定有环,而且环与环之间不相交。那么从任意一个点出发,它最后一定会到达一个环。

只需要求出有向图中所有的环,每个环贡献一个权值最小的点即可。

 

代码

/*
AUTHOR: maxkibble
LANG: c++
PROB: cf 1027D
*/

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;

int n, c[maxn], a[maxn], v[maxn], ans;
vector<int> cir;

void dfs(int x) {
    v[x] = 1;
    if (v[a[x]] == 1) cir.push_back(x);
    else if (v[a[x]] == 0) dfs(a[x]);
    v[x] = 2;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) if (!v[i]) dfs(i);
    for (int i: cir) {
        int x = a[i], minv = c[i];
        while (x != i) {
            minv = min(minv, c[x]);
            x = a[x];
        }
        ans += minv;
    }
    printf("%d\n", ans);
    return 0;
}

 

mstar PQ软件修改方法 1.修改ADC的值 Static code APIXC_AdcGainOffsetSetting tADCSetting[ADC_SET_NUMS]= #define INIT_ADC_RGB_RED_GAIN               0x1000 #define INIT_ADC_RGB_GREEN_GAIN           0x1000 #define INIT_ADC_RGB 阅读详情

相关推荐

hcaptcha无感验证码逆向实战:从接口分析到环境补全

本文详细解析了hcaptcha无感验证码的逆向实战过程。通过分析核心接口与加密逻辑,重点介绍了采用环境补全策略来模拟真实浏览器环境,从而绕过验证码检测。文章提供了从接口抓包、剖析hsw.js加密核心到使用Node.js搭建模拟环境的完整步骤,为自动化操作提供了一种稳健的解决方案。

weixin_29324401的博客 1151

D. Mouse Hunt(思维,找环)

代码只有短短的20行,但是却很巧妙代码只有短短的20行,但是却很巧妙代码只有短短的20行,但是却很巧妙 首先整个图分为环和链首先整个图分为环和链首先整个图分为环和链 环的花费是环内最小点的花费环的花费是环内最小点的花费环的花费是环内最小点的花费 链的花费是链上最后一点的花费链的花费是链上最后一点的花费链的花费是链上最后一点的花费 所以我们从点1到点n去循环 每次都沿着路径去找下一个已经被访问过的点并标记每次都沿着路径去找下一个已经被访问过的点并标记每次都沿着路径去找下一个已经被访问过的点并标记 当我们找到已

jziwjxjd的博客 682

FPGA之VGA转HDMI之编码模块的编写

VGA 图像信息到 HDMI 图像信息的转化需要对输入的 VGA 图像信息进行编码、并行串行转换、单端信号转差分信号、单沿采样转双沿采样。 其中,编码模块就是为了完成 VGA 图像数据 8b 转 10b 的编码。其结构框图如下所示: 图1 编码模块框图 图2 编码模块输入输出信号功能框图 图3 编码模块参考流程图 图4 编码模块参考流程图参数说明 下面我们依据HDMI官方手册中的流程图(图3)进行 HDMI 8b...

qq_51264805的博客 3023

D. Mouse Hunt(思维+tarjan缩点)

https://codeforces.com/problemset/problem/1027/D 思路: 建好有向图后发现把夹子放没有出度的点就好了。且这个没有出度是缩点后的点,费用是缩点里面的最小值费用 #include<iostream> #include<vector> #include<queue> #include<stack> #include<cstring> #include<cmath> #include&lt

代码笔记本 182

《机器学习》周志华_自学记录

第一章绪论## 1.1引言机器学习所研究的主要内容,是关于在计算机上从数据中产生"模型"(model)的算法,即"学习算法"(learning algorithm).在面对新的情况时(例如看到一个没剖开的西瓜),模型会给我们提供相应的判断(例如好瓜).如果说计算机科学是研究关于"算法"的学问,那么类似的,可以说机器学习是研究关于"学习算法"的学问.## 1.2基本术语:用于预测离散值,例如将样本分为“好瓜”或“坏瓜”。:用于预测连续值,例如西瓜的成熟度(如0.95、0.37)。

2201_75296678的博客 2531

AVL-Excite新手必看:5步搞定发动机阀系系统建模(附B站视频教程)

本文为AVL-Excite新手提供发动机阀系系统建模的5步详细指南,涵盖软件环境搭建、基本结构建立、关键参数设置、仿真分析及常见问题排查。通过清晰的步骤说明和实用技巧,帮助工程师快速掌握建模方法,并附有精选B站视频教程链接,便于实践学习。

weixin_29305313的博客 508

Codeforces 1027 D. Mouse Hunt(并查集、dfs)

#include&lt;bits/stdc++.h&gt; using namespace std; #define sc(d) scanf("%d",&amp;d) #define ll long long int c[200005]; int g[200005]; int n; struct DisjointSet { vector&lt;int&gt;father, rank; ...

韵意丶魔法甜点城堡 768

11.1华为OD机试真题 新系统 - 整理学生编号 (Java/Py/C/C++/Js/Go)

11.1华为OD机试真题 新系统 - 整理学生编号 (Java/Py/C/C++/Js/Go)

算法大师! 382

codeforces 1027D Mouse Hunt 【并查集】【dfs】

D. Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Medicine faculty of Berland State University has just finished thei...

Head_Hard的博客 644

【2025软考高级架构师】论文篇——4、论可靠性系统中软件容错技术的应用

软件容错技术对软件的稳定性起着至关重要的作用,尤其是针对互联网性质的软件并发高存在流量峰值等问题,软件容错技术的应用的重要性就不言而喻了。经过这次我所采用的软件容错技术的方法和措施的实施效果后,使我也看到了自己身上的不足之处,我会在今后的架构设计过程中,不断更新自己的知识,不断完善自己的架构设计领域,设计出更好的软件架构,更好的支撑业务平台的运行,提高公司的竞争力,为公司为社会尽一份绵薄之力。

qq837993702的博客 618

cesium 淹没分析

cesium淹没分析

God's asylum的博客 480

Codeforces Round 49 (Rated for Div. 2)D Mouse Hunt(dfs,思维,WA)

#include&amp;amp;lt;iostream&amp;amp;gt; #include&amp;amp;lt;cstdio&amp;amp;gt; using namespace std; const int max_n=200005; const int inf=0x3f3f3f3f; int n; int c[max_n]; int a[max_n]; int in[max_n]; bool visit[max_n]; int ans=0;...

xx_ii 499

【RK3399 Ubuntu】基于rk3399的lxde的lubuntu桌面移植适配记录之第二步:最基本Ubuntu根文件系统的相关常用工具完善

文章目录1.使能系统网络管理服务1.1 安装驱动modules管理工具1.2 使能网络服务2.终端自动补全命令功能3.安装ssh服务4.安装系统信息查看工具5.安装resize工具6.安装lrzsz工具7.使能rc.local服务7.1 修改服务Install块7.2 使能rc.local.service服务7.3 添加执行脚本且赋予执行权限 续接上文,本文主要完善前面已经制作好的rootfs 1.使能系统网络管理服务 1.1 安装驱动modules管理工具 apt-get install kmod 1.

wang 恒 的博客 1385

CF1027D Mouse Hunt 题解

CF1027D Mouse Hunt 图论-强联通分量-Tarjan 根据题目给出小老鼠去向的方式,我们可以发现每一个点只有一条出边,而这些边构成的图就是一条链 对于这条链,我们发现链的尾端有可能为一个环(如下图) 又因为每个点只能有一条出边,所以这个环不可能在链的中间 这样,答案就被转换成了链尾端的强联通分量中的最小点权 #include<cstdio> #include<i...

Brian_Pan_的博客 307

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

CF1027D Mouse Hunt(dfs+图论)

CF1027D 文章目录CF1027D题意输入输出解析代码启示 题意 n间宿舍里有老鼠,老鼠(只有一只)可能出现在任何一间寝室,然后它在第i个寝室里待一秒后一定会跑向第c[i]个寝室,给出在每个寝室布置陷阱的成本,求无论老鼠初始出现在那个寝室,一定会被抓住的最低成本. 输入 第一行给出n(1<=n<=2e5) 第二行给出n个数字,每个大小都是[1,1e4],第i个表示c[i],即在...

Dawn-K の Blog 384

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

CF1027D Mouse Hunt题解

题目: 伯兰州立大学的医学部刚刚结束了招生活动。和以往一样,约80%的申请人都是女生并且她们中的大多数人将在未来4年(真希望如此)住在大学宿舍里。 宿舍楼里有nn个房间和一只老鼠!女孩们决定在一些房间里设置捕鼠器来除掉这只可怕的怪物。在ii号房间设置陷阱要花费c_ic i ​ 伯兰币。房间编号从11到nn。 要知道老鼠不是一直原地不动的,它不停地跑来跑去。如果tt秒时它在ii号房间,那么...

ShineEternal的笔记小屋 366

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

CF1027D Mouse Hunt(topo总结)

题意:有n个房间和若干只老鼠(该题可直接当做一只处理),给定n值,以及每个房间来老鼠后下一个会去的房间号,还给了每个房间布置陷阱所需花的成本,求能让老鼠最终落网的布置陷阱的最小成本 本题需要用的数据结构就是基础的图论,用个next数组存下个邻接节点,然后在线存好数据后,我们需要注意,有n个节点也有n个边,那必然会有回路,其余是链的形式,我们用topo排序挨个去掉入度为0的链,剩余环(不可能剩多余链的),再用dfs统计最小值,即为ans AC代码 #include<cstdio> #include

rainbowower的博客 389

CF1027D Mouse Hunt 思维

Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Medicine faculty of Berland State University has just fi...

weixin_34342578的博客 156

Mouse Hunt(CF-1027D)

Problem Description Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80% of applicants are girls and majority of them are going to live in the...

Alex_McAvoy的博客 563

题解 CF1027D 【Mouse Hunt】

这道题原本写了一个很复杂的DFS,然后陷入绝望的调试。 看了一下题解发现自己完全想复杂了。 这里大概就是补充一些题解没有详细解释的代码吧。。。 (小声BB)现在最优解rank4(话说$O2$负优化什么鬼啊) 1 read(n); 2 for(register int i=1;i<=n;++i)read(c[i]); 3 for(register int i=1...

weixin_33697898的博客 146

(CF)Educational Codeforces Round 49

红红火火恍恍惚惚,哈哈哈哈上分喽,上了60整o(* ̄▽ ̄*)ブ,蓝蓝的~ 话说昨晚打的也并不十分顺利,CD都WA了好几发QAQ索性没挂终测不然真是凉了~ A、Palindromic Twist http://codeforces.com/contest/1027/problem/A 题意:一串字符串,每个字符都必须上或下移动一个位子,AZ只有一种移动方法。求能否构成回文串 思路:大水题,...

AAAAAAAC的博客 257

D. Mouse Hunt(环)

这道题搞明白了好开心啊!!!感激LY大佬!!! http://codeforces.com/contest/1027/problem/D D. Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outp...

20164225的博客 784

补题向 | Mouse Hunt(图)

Mouse Hunt n个房间,每个房间只通往(单向)另外一个房间(可能是自己),每个房间设陷阱花费的金额为ci,有一只老鼠,可能在任何一个房间,求为保证一定能捉住老鼠,设陷阱的最小花费金额 将每个房间看为点,每个点只有一条出边或没有出边,陷阱只需要设在两类点上 第一类点是没有出边的点,所有点将其入边保存为反向边,当找到没有出边的点时,顺着反向边删除所有能到达此点的入点(标记) 把...

bekote的博客 627
上一篇: cf 1012B Chemical table
下一篇: cf 1027f Session in BSU
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值