zoukankan      html  css  js  c++  java
  • bzoj2071: [POI2004]JAS

    Description

    在Byteotia有一个洞穴. 它包含n 个洞室和一些隧道连接他们. 每个洞室之间只有一条唯一的路径连接他们. Hansel 在其中一个洞室藏了宝藏, 但是它不会说出它在哪. Gretel 想知道. 当她询问一个洞室是否有宝藏时,如果她猜对了Hansel 会告诉她,如果猜错了他会告诉她哪个方向会有宝藏. 给出洞穴的信息,那么无论Hansel 把宝藏藏在了哪,求出最少要询问多少次才能找到宝藏.

    Input

    输入一个数n, 1<= n <= 50,000. 表示洞室总数,接下来n-1 行描述n – 1条边.

    Output

    输出一个数表示最少询问次数.

    Sample Input


    5
    1 2
    2 3
    4 3
    5 3

    Sample Output

    2
     
     
     
    code:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #define maxn 50005
     7 using namespace std;
     8 char ch;
     9 int n,m,a,b,s1[20],f[maxn],sta[maxn],tot,now[maxn],son[maxn<<1],pre[maxn<<1];
    10 bool ok;
    11 void read(int &x){
    12     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
    13     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    14     if (ok) x=-x;
    15 }
    16 void put(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b;}
    17 void dfs(int u,int fa){
    18     int s2=0,cu=0,deg=0;
    19     for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
    20         if (v!=fa) dfs(v,u),s2|=sta[v],deg++;
    21     if (!deg){f[u]=0,sta[u]=1;return;}
    22     memset(s1,0,sizeof(s1));
    23     for (int p=now[u],v=son[p];p;p=pre[p],v=son[p]) if (v!=fa)
    24         for (int i=0;i<m;i++) if (sta[v]&(1<<i)) s1[i]++;
    25     if (deg>1) for (int i=m-1;i>=0;i--) if (s1[i]>=2){cu=i+1;break;}
    26     for (int i=cu;i<m;i++) if (!(s2&(1<<i))){cu=i;break;}
    27     sta[u]=((s2>>cu)|1)<<cu,f[u]=cu;
    28     for (int p=now[u],v=son[p];p;p=pre[p],v=son[p]) if (v!=fa) f[u]=max(f[u],f[v]);
    29 }
    30 int main(){
    31     read(n),m=log2(n)+1;
    32     for (int i=1;i<n;i++) read(a),read(b),put(a,b),put(b,a);
    33     dfs(1,0);
    34     printf("%d
    ",f[1]);
    35     return 0;
    36 }
  • 相关阅读:
    统计SQL
    记一次惨痛教训
    window ssh 连接 本地虚拟机ubuntu 16
    js undefined 笔记
    java new java.text.SimpleDateFormat("yyyyMM01").format(date)
    java 反射
    mapinfo使用
    EnableAutoConfiguration注解说明
    Eureka Server 代码分析01
    BigDecimal 学习比较
  • 原文地址:https://www.cnblogs.com/chenyushuo/p/4917881.html
Copyright © 2011-2022 走看看