Description has a cave in Byteotia. It contains n chambers and some tunnels connect them. There is only one unique path between each of the Chambers to connect them. Hansel hid the treasure in one of the chambers, but it would not say where it was. Gretel want to know. When she asks if there is a treasure in a cave room, if she guessed it, Hansel would tell her if he guessed wrong, he would tell her which direction there would be a treasure. Given the cave's information, no matter where the Hansel hid the treasure, find out how many times to ask for the treasure. Input inputs a number n, 1<= n <= 50,000. Represents the total number of cavities, and the next n-1 line describes the n–1 edge. A number of output outputs indicates the minimum number of queries. Sample Input
5
1 2
2 3
4 3
5 3
Sample Output2
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 500057 using namespacestd;8 Charch;9 intn,m,a,b,s1[ -],f[maxn],sta[maxn],tot,now[maxn],son[maxn<<1],pre[maxn<<1];Ten BOOLOK; One voidReadint&x) { A for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; - for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); - if(OK) x=-x; the } - voidPutintAintb) {pre[++tot]=now[a],now[a]=tot,son[tot]=b;} - voidDfsintUintFA) { - intS2=0, cu=0, deg=0; + for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) - if(V!=FA) DFS (v,u), s2|=sta[v],deg++; + if(!DEG) {f[u]=0, sta[u]=1;return;} Amemset (S1,0,sizeof(S1)); at for(intP=now[u],v=son[p];p; p=pre[p],v=son[p])if(v!=FA) - for(intI=0; i<m;i++)if(sta[v]& (1<<i)) s1[i]++; - if(deg>1) for(intj=n-1; i>=0; i--)if(s1[i]>=2) {cu=i+1; Break;} - for(inti=cu;i<m;i++)if(! (s2& (1<<i)) {cu=i; Break;} -Sta[u]= ((S2>>CU) |1) <<cu,f[u]=cu; - for(intP=now[u],v=son[p];p; p=pre[p],v=son[p])if(V!=FA) f[u]=Max (f[u],f[v]); in } - intMain () { toRead (n), M=LOG2 (n) +1; + for(intI=1; i<n;i++Read (a), read (b), put (a. a), put (b,a); -Dfs1,0); theprintf"%d\n", f[1]); * return 0; $}
bzoj2071: [Poi2004]jas