1. 程式人生 > >Codeforces 1027D(Tarjan縮點+貪心)

Codeforces 1027D(Tarjan縮點+貪心)

傳送門

題面:

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,問你消滅掉所有老鼠的最少花費。

題目分析:

    我們考慮將每個老鼠的移動軌跡都建成圖,我們可以發現,因為每個點最多隻有一個出度,而且可以,因此最終會形成一個基環內向樹。而在基環內向樹上的每一個環上的影響都是相同的,因此我們需要用Tarjan縮點,並重構圖。

    之後我們只需要找到重構圖中出度為0的點,(因為這種點要麼是孤點,要麼是眾多點的匯聚點),貪心的選取該點所在的強連通分量中點權最小的點即可。

程式碼:

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;
struct edge{
    int to,next;
}q[maxn];
int head[maxn],cnt=0;
int low[maxn],dfn[maxn],belong[maxn];
int index,top;
int tot;
bool vis[maxn];
int belong_num[maxn];
int val[maxn];
vector<int>vec[maxn];
int outde[maxn];
stack<int>st;
void add_edge(int from,int to){
    q[cnt].next=head[from];
    q[cnt].to=to;
    head[from]=cnt++;
}
void tarjan(int x){//Tarjan縮點
    dfn[x]=low[x]=++tot;
    vis[x]=1;
    st.push(x);
    for(int i=head[x];i!=-1;i=q[i].next){
        edge e=q[i];
        if(!dfn[e.to]){
            tarjan(e.to);
            low[x]=min(low[e.to],low[x]);
        }
        else if(vis[e.to]==1){
            low[x]=min(low[x],dfn[e.to]);
        }
    }
    if(dfn[x]==low[x]){
        int v;
        index=index+1;
        do{
            v=st.top();
            st.pop();
            belong[v]=index;
            belong_num[index]++;
            vis[v]=0;
        }while(v!=x);
    }
}
void init(){
    cnt=tot=index=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(outde,0,sizeof(outde));
    memset(belong_num,0,sizeof(belong_num));
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
}
int num[maxn];
int main()
{
    int n;
    init();
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&val[i]);
    }
    for(int i=1;i<=n;i++){
        int id;
        scanf("%d",&id);
        add_edge(i,id);
    }
    for(int i=1;i<=n;i++){
        if(dfn[i]==0) tarjan(i);
    }
    for(int i=1;i<=n;i++){//統計同一個連通分量中的點權
        vec[belong[i]].push_back(val[i]);
        for(int j=head[i];j!=-1;j=q[j].next){
            if(belong[i]!=belong[q[j].to]) outde[belong[i]]++;//增加出度
        }
    }
    ll res=0;
    for(int i=1;i<=index;i++){
        if(outde[i]==0){
            sort(vec[i].begin(),vec[i].end());
            res+=vec[i][0];
        }
    }
    cout<<res<<endl;
}