zoukankan      html  css  js  c++  java
  • BZOJ5189: [Usaco2018 Jan]Cow at Large 贪心+LCA

    BZOJ没有题面QAQ,题目链接

    洛谷有:题目链接

    这题首先要读懂题..(洛谷的翻译有点迷

    就是指定根节点,然后可以在叶子结点放个人,然后奶牛在根,问最少要在叶子结点放多少人才能让奶牛走不到叶子结点(奶牛和人相遇就死掉)

    首先对于一个叶子结点和另外一个叶子结点,只需要在其中一个节点放人的条件:当且仅当$2*dep[lca(u,v)]>=dep[u]$($u,v$是两个节点)

    所以把所以叶子结点扔进一个set里面,每次取出深度最小的

    然后遍历一下剩下的叶子结点,对于满足上面那个条件的叶子结点全删了,统计一下答案就可以了

    #include <bits/stdc++.h>
    
    using namespace std ;
    
    #define N 100010
    #define inf 0x3f3f3f3f
    
    int dep[ N ] , fa[ N ] , top[ N ] , siz[ N ] ;
    int root , n ;
    int head[ N ] , cnt ;
    int in[ N ] ;
    
    struct edge {
        int to , nxt ;
    } e[ N << 1 ] ;
    struct node {
        int dep , id ;
    } ;
    
    bool operator < ( const node &x , const node &y ) {
        return x.dep == y.dep ? x.id < y.id : x.dep < y.dep ;
    }
    set< node > s ;
    
    void ins( int u , int v ) {
        e[ ++ cnt ].to = v ;
        e[ cnt ].nxt = head[ u ] ; 
        head[ u ] = cnt ;
    }
    
    void dfs1( int u ) {
        siz[ u ] = 1 ;
        if( in[ u ] == 1 ) s.insert( (node){ dep[ u ] , u } ) ;
        for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
            if( e[ i ].to == fa[ u ] ) continue ;
            fa[ e[ i ].to ] = u ;
            dep[ e[ i ].to ] = dep[ u ] + 1 ;
            dfs1( e[ i ].to ) ;
            siz[ u ] += siz[ e[ i ].to ] ;
        }
    }
    
    void dfs2( int u , int topf ) {
        top[ u ] = topf ;
        int k = 0 ;
        for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
            if( dep[ e[ i ].to ] > dep[ u ] && siz[ e[ i ].to ] > siz[ k ] ) 
                k = e[ i ].to ;
        }
        if( !k ) return ;
        dfs2( k , topf ) ;
        for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
            if( k != e[ i ].to && dep[ e[ i ].to ] > dep[ u ] ) 
                dfs2( e[ i ].to , e[ i ].to ) ;
        }
    }
    
    int lca( int x , int y ) {
        while( top[ x ] != top[ y ] ) {
            if( dep[ top[ x ] ] < dep[ top[ y ] ] ) swap( x , y ) ;
            x = fa[ top[ x ] ] ;
        }
        if( dep[ x ] > dep[ y ] ) swap( x , y ) ;
        return x ;
    }
    
    int main() {
        scanf( "%d%d" , &n , &root ) ;
        for( int i = 1 , u , v ; i < n ; i ++ ) {
            scanf( "%d%d" , &u , &v ) ;
            ins( u , v ) ; ins( v , u ) ;
            in[ u ] ++ , in[ v ] ++ ;
        }
        if( in[ root ] == 1 ) return puts( "1" ) , 0 ; 
        dfs1( root ) ;
        dfs2( root , root ) ;
        int ans = 0 ;
        while( !s.empty() ) {
            node u = *s.begin() ;
            s.erase( u ) ;
            ans ++ ;
            for( set<node>::iterator it = s.begin() ; it != s.end() ; ) {
                node v = *it ;
                it ++ ;
                if( dep[ u.id ] <= 2 * dep[ lca( u.id , v.id ) ] ) {
                    s.erase( v ) ;
                }
            }
        }
        printf( "%d
    " , ans ) ;
    } 
  • 相关阅读:
    延迟为程序集签名
    bootshrap会改变IE浏览器滚动条样式
    Spark算子选择策略
    kafka常用系统命令-1
    数据结构-树
    1.PyCharm 用法
    sql 语句用法
    linux命令
    linux命令
    es的相关知识二(检索文档)
  • 原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ5189.html
Copyright © 2011-2022 走看看