zoukankan      html  css  js  c++  java
  • The Flee Plan of Groundhog

    The Flee Plan of Groundhog
    dfs
    相向而行就距离-3,时间+1
    背道而驰就距离-1,时间+1
    注意土拨鼠可以不走,特判就好

    #include <bits/stdc++.h>
    #define inf 2333333333333333
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(int i=a;i<=b;++i)
    //by war
    //2020.8.8
    using namespace std;
    int n,s,t,x,y,ans;
    int d[N];
    struct node{
        int n;
        node *next;
    }*e[N];
    
    void in(int &x){
        int y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(int x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(int x,int y){
        node *p;
        p=new node();
        p->n=y;
        if(e[x]==0)
            e[x]=p;
        else{
            p->next=e[x]->next;
            e[x]->next=p;
        }
    }
    
    void dfs1(int x,int fa,int deep){
        d[x]=deep;
        for(node *i=e[x];i;i=i->next){
            if(i->n==fa) continue;
            dfs1(i->n,x,deep+1);
        }
    }
    
    void dfs2(int x,int fa,int deep){
        if(deep==t){
            s=x;
            return;
        }
        for(node *i=e[x];i;i=i->next){
            if(i->n==fa || d[i->n]>=d[x]) continue;
            dfs2(i->n,x,deep+1);
        }
    }
    
    void dfs3(int x,int fa,int dis,int t){
        if(dis<=0){
            ans=max(ans,t);
            return;
        }
        for(node *i=e[x];i;i=i->next){
            if(i->n==fa) continue;
            if(d[i->n]<d[x]){
                dfs3(i->n,x,dis-3,t+1);
            }else{
                dfs3(i->n,x,dis-1,t+1);
            }
        }
        if(dis&1) ans=max(ans,t+dis/2+1);
        else ans=max(ans,t+dis/2);
    }
    
    signed main(){
        in(n);in(t);
        For(i,1,n-1){
            in(x);in(y);
            push(x,y);
            push(y,x);
        }
        dfs1(n,n,0);
        dfs2(1,1,0);
        dfs3(s,s,d[s],0);
        o(ans);
        return 0;
    }
  • 相关阅读:
    Oracle9i数据库移动过程
    基于索引的SQL语句优化之降龙十八掌
    activex发布步骤
    用ftpsupport进行ftp上传
    周五晚上看了变形金刚
    故宫游
    UTF8转GB2312
    跨数据库的视图【自己留着看】
    数学之美 - 布隆过滤器(Bloom Filter)【转】
    搜索引擎优化SEO的ppt讲义
  • 原文地址:https://www.cnblogs.com/war1111/p/13458050.html
Copyright © 2011-2022 走看看