zoukankan      html  css  js  c++  java
  • LuoguP5008 [yLOI2018] 锦鲤抄 tarjan+贪心

    首先,如果是一个 DAG 的话入度不为 0 的点肯定可以都选.       

    然后如果是一般图的话我们缩点,考虑对于一个强连通分量如何处理:  

    如果该强连通分量入度为 0 ,那么一定有一个点不能选,其他点都能选.  

    如果该强连通分量入读不为 0,那么肯定所有点都可以选.    

    由于缩完点后是一个 DAG 的形式,我们一定可以按照拓扑序从后往前来选.   

    这样就可以在上述两个限制下随便选了.   

    code: 

    #include <cstdio>
    #include <vector>  
    #include <stack>
    #include <cstring>
    #include <algorithm>  
    #define ll long long 
    #define N 500009  
    #define M 2000004   
    #define pb push_back  
    #define setIO(s) freopen(s".in","r",stdin) 
    using namespace std;  
    int n,m,K,edges,scc,tim; 
    stack<int>sta;
    vector<int>G[N];
    int dfn[N],low[N]; 
    int w[N],hd[N],to[M],nex[M],deg[N],a[N],id[N]; 
    void add(int u,int v) { 
        nex[++edges]=hd[u]; 
        hd[u]=edges,to[edges]=v; 
    }
    void tarjan(int x) {    
        sta.push(x);   
        dfn[x]=low[x]=++tim;   
        for(int i=hd[x];i;i=nex[i]) { 
            int y=to[i]; 
            if(!dfn[y]) { 
                tarjan(y),low[x]=min(low[x],low[y]); 
            } 
            else if(!id[y]) {   
                low[x]=min(low[x],dfn[y]); 
            }
        }    
        if(low[x]==dfn[x]) { 
            ++scc;  
            for(;;) { 
                int u=sta.top();sta.pop();  
                id[u]=scc;  
                G[scc].pb(w[u]);  
                if(u==x) break;  
            }
        }
    }
    int main() { 
        // setIO("input");     
        int x,y,z; 
        scanf("%d%d%d",&n,&m,&K); 
        for(int i=1;i<=n;++i) {     
            scanf("%d",&w[i]); 
        }  
        for(int i=1;i<=m;++i) { 
            scanf("%d%d",&x,&y); 
            add(x,y); 
        }      
        for(int i=1;i<=n;++i) { 
            if(!dfn[i]) tarjan(i); 
        }
        for(int i=1;i<=scc;++i) { 
            sort(G[i].begin(),G[i].end());  
        }   
        int cnt=0; 
        ll ans=0;    
        for(int i=1;i<=n;++i) {     
            for(int j=hd[i];j;j=nex[j]) { 
                int y=to[j]; 
                if(id[y]==id[i]) continue;      
                ++deg[id[y]];  
            }
        }
        for(int i=1;i<=scc;++i) { 
            if(deg[i]) {    
                for(int j=0;j<G[i].size();++j) a[++cnt]=G[i][j]; 
            } 
            else {  
                for(int j=1;j<G[i].size();++j) a[++cnt]=G[i][j]; 
            } 
        } 
        sort(a+1,a+1+cnt); 
        for(int i=1;i<=min(K,cnt);++i) { 
            ans+=a[cnt-i+1]; 
        }
        printf("%lld
    ",ans); 
        return 0; 
    }
    

      

  • 相关阅读:
    【数学】Codeforces Round #470 (Div2) B
    【数学】At Coder 091 D题
    【2-SAT】The Ministers’ Major Mess UVALive – 4452
    【二分答案+2-SAT】Now or later UVALive
    【栈模拟dfs】Cells UVALive
    浅谈2-SAT(待续)
    【交叉染色法判断二分图】Claw Decomposition UVA
    【拓扑排序或差分约束】Guess UVALive
    【欧拉回路】UVA
    周总结8.15
  • 原文地址:https://www.cnblogs.com/guangheli/p/13476641.html
Copyright © 2011-2022 走看看