zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
    simlescalar CPU模拟器源代码分析
    基于webRTC技术 音频和视频,IM解
    SVM明确的解释1__ 线性可分问题
    Atititjs javascript异常处理机制java异常转换.js exception process
    hibernate annotation 相关主键生成策略
    切向量,普通矢量,渐变
    C++动态数组简单的模拟二元堆
    [Angular] Create a custom validator for reactive forms in Angular
    [RxJS] Marbles Testings
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007204.html
Copyright © 2011-2022 走看看