zoukankan      html  css  js  c++  java
  • 【题解】P2279消防局的设立

    【题解】[P2279 HNOI2003]消防局的设立

    又是一道贪心。

    随便指定一个点为根,可以知道在覆盖了一个节点的子树的情况下,消防站越高越好。那么我们就贪心吧。(trick)是按深度(push)(queue)里,然后直接取出来判断、贪心。

    咕咕咕

    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<cstdlib>
    #include<vector>
    #include<set>
    #include<map>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<bitset>
    #include<ctime>
    
    using namespace std;
    
    
    #define TMP template < class ins >
    #define endl '
    '
    #define RP(t,a,b) for(register int t=(a),edd=(b);t<=edd;t++)
    #define ERP(t,a) for(register int t=head[(a)];t;t=e[t].nx)
    #define DRP(t,a,b) for(register int t=(a),edd=(b);t>=edd;t--)
    typedef long long ll;
    
    TMP inline ins qr(ins tag){
        char c=getchar();
        ins x=0;
        int q=0;
        while(c<48||c>57)
        q=c==45?-1:q,c=getchar();
        while(c>=48&&c<=57)
        x=x*10+c-48,c=getchar();
        return q==-1?-x:x;
    }
    const int maxn=1005;
    struct E{
        int to,nx;
    }e[maxn<<1];
    int head[maxn];
    int Cnt;
    
    inline void add(int fr,int to,bool f){
        e[++Cnt]=(E){to,head[fr]};
        head[fr]=Cnt;
        if(f)
        add(to,fr,0);
    }
    int n;
    int maxd,ans;
    int t1, t2 ,t3;
    int r[maxn];
    int d[maxn];
    bool cov[maxn];
    vector < int > dpt[maxn];
    #define pb push_back
    
    
    void dfs(int now,int last){
        d[now]=d[last]+1;
        r[now]=last;
        maxd=max(maxd,d[now]);
        dpt[d[now]].pb(now);
        ERP(t,now)
        if(e[t].to!=last)
            dfs(e[t].to,now);
        
    }
    
    void dfs2(int now,int sum){
        cov[now]=1;
        if(!sum)
        return;
        ERP(t,now)
        dfs2(e[t].to,sum-1);
    }
    
    queue < int > q;
    inline void eff(){
        DRP(t,maxd,1)
        RP(i,0,dpt[t].size()-1)
        q.push(dpt[t][i]);
        while(!q.empty()){
        int temp=q.front();
        q.pop();
        //cout<<temp<<endl;
        if(!cov[temp])
            ans++,dfs2(r[r[temp]],2);
        }
        cout<<ans<<endl;
    }
    
    
    inline void init(){
        n=qr(1);
        RP(t,2,n){
        t1=qr(1);
        add(t,t1,1);
        }
        dfs(1,1);
        r[1]=1;
        eff();
    }
    
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("in.in","r",stdin);
        freopen("out.out","w",stdout);
    #endif
        return init(),0;
    }
    
    
  • 相关阅读:
    ORA-00904:标识符无效
    SQL错误:ORA-12899
    ORA-01722:无效数字
    科学记数法
    报表软件公司悬赏 BUG,100块钱1个的真实用意
    Perl--包
    Perl--正则
    Perl use strict 控制变量
    Oracle不删除用户,导入数据
    从别人的角度理解这个世界——Leo鉴书80
  • 原文地址:https://www.cnblogs.com/winlere/p/10335749.html
Copyright © 2011-2022 走看看