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个房间和一个老鼠,老鼠一开始可能在任意一个房间,老鼠会从房间i跳到a[i]房间,问在哪些房间下陷阱可以用最小的代价抓到老鼠?

分析:遍历所有房间,给从这些可能到达的房间打上标记,每次打标志在花费最小的房间布下陷阱

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, ans, a[maxn], c[maxn], vis[maxn];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for( ll i = 1; i <= n; i ++ ) {
cin >> c[i];
}
for( ll i = 1; i <= n; i ++ ) {
cin >> a[i];
}
for( ll i = 1; i <= n; i ++ ) {
ll x = i, v, mn;
while( !vis[x] ) { // 找到已被访问过的下一个房间
vis[x] = i, x = a[x];
}
if( vis[x] != i ) {
continue;
}
v = x, mn = c[x];
while( a[x] != v ) { //在可能经过的所有房间需要花费最小的房间下陷阱
x = a[x], mn = min( mn, c[x] );
}
ans += mn;
}
cout << ans << endl;
return 0;
}

  

CF1027D Mouse Hunt 思维的更多相关文章

  1. CF1027D Mouse Hunt题解

    题目: 伯兰州立大学的医学部刚刚结束了招生活动.和以往一样,约80%的申请人都是女生并且她们中的大多数人将在未来4年(真希望如此)住在大学宿舍里. 宿舍楼里有nn个房间和一只老鼠!女孩们决定在一些房间 ...

  2. 【Edu49 1027D】 Mouse Hunt DFS 环

    1027D. Mouse Hunt:http://codeforces.com/contest/1027/problem/D 题意: 有n个房间,每个房间放置捕鼠器的费用是不同的,已知老鼠在一个房间x ...

  3. Mouse Hunt

    Mouse Hunt 给定一个n个点的图,每个点有权值\(c_i\),并且只有一条出边.现在你要在一些点上打标记,使得从任何一个点出发最终都会经过有标记的点.求标记点的权值和最小值. 就是找环啊!拓扑 ...

  4. Codeforces B. Mouse Hunt(强连通分解缩点)

    题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Mouse Hunt CodeForces - 1027D(思维 找环)

    Medicine faculty of Berland State University has just finished their admission campaign. As usual, a ...

  6. 【CF1027D】Mouse Hunt(拓扑排序,环)

    题意:给定n个房间,有一只老鼠可能从其中的任意一个出现, 在第i个房间设置捕鼠夹的代价是a[i],若老鼠当前在i号房间则下一秒会移动到b[i]号, 问一定能抓住老鼠的最小的总代价 n<=2e5, ...

  7. 题解 CF1027D 【Mouse Hunt】

    这道题原本写了一个很复杂的DFS,然后陷入绝望的调试. 看了一下题解发现自己完全想复杂了. 这里大概就是补充一些题解没有详细解释的代码吧... (小声BB)现在最优解rank4(话说$O2$负优化什么 ...

  8. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  9. 【Codeforces 1027D】Mouse Hunt

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 先求出来强连通分量. 每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就ok了 [代码] #include <bits/st ...

随机推荐

  1. 【iOS】no identity found Command /usr/bin/codesign failed with exit code 1

    今天遇到了这个问题,详情如下图: 后来发现是自己脑子短路了……只添加了 Provisioning Profiles, 而忘记添加 Certificates, 就是下面的两个:

  2. Mybatis与Spring集成时都做了什么?

    Mybatis是java开发者非常熟悉的ORM框架,Spring集成Mybatis更是我们的日常开发姿势. 本篇主要讲Mybatis与Spring集成所做的事情,让读过本文的开发者对Mybatis和S ...

  3. 完美解决windows10磁盘占用100%并出现卡顿、假死无反应

    完美解决windows10磁盘占用100%并出现卡顿.假死无反应 想必大家也跟我一样,自从用win10系统以后经常会出现这种情况:磁盘突然占用100%然后开始出现假死现象,电脑卡住,点击任何软件没反应 ...

  4. ipv6的连接

    基础知识不说了,网上一大堆! 基本内容不说了,写字太累了! 只说三点细节,记住就行: 1.ff开头的是多播地址,只能用于udp多播 2.fe80开头的是本地link地址,不管ping也好,connec ...

  5. PythonDay05

    第五章 今日内容 字典 字典 语法:{'key1':1,'key2':2} 注意:dict保存的数据不是按照我们添加进去的顺序保存的. 是按照hash表的顺序保存的. ⽽hash表 不是连续的. 所以 ...

  6. 【原创】JAVA进程突然消失的原因?

    引言 值此七夕佳节,烟哥放弃了无数妹纸的邀约,坐在电脑面前码字,就是为了给读者带来新的知识,这是一件伟大的事业! 好吧,实际情况是没人约.为了化解尴尬,我决定卖力写文章,嗯,一定是我过于屌丝! 好了, ...

  7. Usaco Training [2.1] The Castle 搜索

    传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...

  8. Canvas动画(PC端 移动端)

    Canvas动画(PC端 移动端) 一,介绍与需求 1.1,介绍 canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3 ...

  9. 19 个 JavaScript 编码小技巧

    这篇文章适合任何一位基于JavaScript开发的开发者.我写这篇文章主要涉及JavaScript中一些简写的代码,帮助大家更好理解一些JavaScript的基础.希望这些代码能从不同的角度帮助你更好 ...

  10. 如何保证FPGA PCIe唤醒能满足PC的100ms 的时间要求(Autonomous Mode)?

    原创By DeeZeng [ Intel FPGA笔记 ]  PC 需要PCIe设备在 100ms 内启动,这样PC 才能扫描到PCIe 设备.对于 FPGA PCIe 板卡,同样也需要满足这个时间要 ...