Codeforces 1027D 图论

题目:

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个房间内乱窜,当老鼠在第i个房间内时,它接下来会出现在ai房间。在第i个房间安防捕鼠器的代价为ci,问最少需要花多少钱安防捕鼠夹,才能保证抓住老鼠。

方法:找环。

首先,这是一幅图,有n个节点和n条边每个结点都有且仅有一条出边,必然存在着环。

第二,老鼠无论沿何种路径走,必然会走进一个环中。

由以上两点,我们可以知道,安防捕鼠夹的最好方法,就是在每个环上找一个代价最小的地方,放置捕鼠夹。

代码如下:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int maxn = 200005;
int a[maxn];
int T;
int n;
int x;
int cnt;
int c[maxn];
int visit[maxn];
int ans;
int spos;
int Min;

//深搜找环
void dfs(int pos){
    if (visit[pos] == 2)
        return;
    if (visit[pos] == 1){
        if (pos == spos){
            ans += Min;
            visit[pos] = 2;
        } else if (spos == -1){
            spos = pos;
            Min = c[pos];
            dfs(a[pos]);
            visit[pos] = 2;
        } else {
            Min = min(c[pos],Min);
            dfs(a[pos]);
            visit[pos] = 2;
        }
        return;
    }
    visit[pos] = 1;
    dfs(a[pos]);
    visit[pos] = 2;
    return;
}

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 (visit[i])
            continue;
        Min = 200005;
        spos = -1;
        dfs(i);
    }
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值