萌新求助
查看原帖
萌新求助
291706
Unordered_OIer楼主2020/12/26 19:36

人没了,板题都切不了了

82 分,WA on #4,#7,#10,皆为与正确答案少 1

使用了各种各样的 log2 全是 82 分。

#include<bits/stdc++.h>
#define N 100005
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
inline void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeln(ll x){write(x),putchar('\n');}
struct edge{ll t,nxt;}es[N<<1];
ll n,hd[N],etot,out[N],ans;
inline void add(ll u,ll v){es[++etot]=(edge){v,hd[u]},hd[u]=etot;}
inline ll log2(ll num){
	ll cnt=0;
	while((1ll<<cnt)<num)cnt++;
	return cnt;
}
inline void dfs(ll u,ll fa){
	ll qv=log2(out[u]);
	ans+=qv+out[u]-(u!=1);
	for(ll i=hd[u],v;i;i=es[i].nxt)
		if((v=es[i].t)!=fa)dfs(v,u);
}
int main(){
	n=read();
	for(ll i=1,u,v;i<n;i++){
		u=read(),v=read();
		add(u,v),add(v,u);
		out[u]++,out[v]++;
	}
	dfs(1,0);
	writeln(ans);
	return 0;
}
2020/12/26 19:36
加载中...