zoukankan      html  css  js  c++  java
  • CCPC2020秦皇岛K题K. Kingdom's Power(贪心)

    题目链接:https://codeforces.ml/gym/102769/problem/K

    题目大意:由根节点派遣军队去占领城市,根节点有无数军队,每次只能移动一支军队,问占领完所有城市的最少时间。

    思路:

    由于每次只能移动一个军队,所以先优先贪心往当前节点深度最短的叶子节点移动,每次到达叶子节点,再比较是当前从根节点派兵的时间花费和不是根节点派兵的时间花费,从中取最小值。

    做法:

    第一遍dfs,将各个节点的边按照叶子节点的深度从小到大排序,第二次dfs统计各个叶子节点对答案的贡献。

    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int N=1e6+7;
    const int M=2e6+7;
    int t,n;
    int head[N],deep[N],du[N],Ans,clik,tot;
    typedef pair<int,int> pll;
    priority_queue <pll,vector<pll>,greater<pll> > q[N];
    struct Edge{
        int to;
        int next;
    }pre[M];
    void add(int u,int v){
        pre[++tot].to=v;
        pre[tot].next=head[u];
        head[u]=tot;
    }
    int df(int u,int fa,int h){
        int res=0,ans=0;
        deep[u]=h;
        for(int i=head[u];i!=0;i=pre[i].next){
            int v=pre[i].to;
            if(v==fa) continue;
            ans=df(v,u,h+1)+1;
            q[u].push(pll(ans,v));
            res=max(res,ans);        
        }
        return res;
    }
    int dfs(int u,int dep){
        while(!q[u].empty()){
            int v=q[u].top().second;
            int cost=q[u].top().first;
            q[u].pop();
            dep=dfs(v,dep+1)+1;
        }
        if(u!=1&&du[u]==1){
            Ans+=min(deep[u],dep);
            dep=0;
        }
        return dep;
    }
    int main(){
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            tot=0;
            for(int i=0;i<=n;i++){
                while(!q[i].empty()){
                    q[i].pop();
                }
                du[i]=deep[i]=head[i]=0;
            }
            for(int i=2;i<=n;i++){
                int f;
                scanf("%d",&f);
                add(f,i),add(i,f);
                du[i]++,du[f]++;
            }
            Ans=0;
            df(1,-1,0);
            dfs(1,0);
            printf("Case #%d: %d
    ",++clik,Ans);
        }
    }
  • 相关阅读:
    制作类似QQ截图软件
    XML文件与实体类的互相转换
    MFC中真彩工具条的制作方法
    MFC使用技巧集锦(1)(转载)
    抽象工厂模式与工厂方法模式区别
    VC数据库编程分析
    如何让工具条显示256色图像
    华为软件编程规范和范例
    设计模式总结性试题
    VC++中基于ADO操作ACCESS数据库,FLEXGRID控件的综合应用
  • 原文地址:https://www.cnblogs.com/kksk/p/13883431.html
Copyright © 2011-2022 走看看