QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#289197#7206. TripleAFewSunsML 0ms0kbC++145.3kb2023-12-23 16:04:532023-12-23 16:04:54

Judging History

你现在查看的是最新测评结果

  • [2023-12-23 16:04:54]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2023-12-23 16:04:53]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
vector<ll> vec[100010],son;
ll t,n,head[100010],cnt=0,ans=0;
ll rt,minn,nsiz,siz[100010];
ll dep[100010],maxdep[100010],dson[100010],top[100010];
ll tot[100010],tot1[100010],tot2[100010],cntt[100010],sum1[100010],sum2[100010];
bl ck[100010];
struct node{
	ll nxt,to;
}e[200020];
void add(ll u,ll v){
	e[++cnt].nxt=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
void getroot(ll fa,ll u){
	siz[u]=1;
	ll maxx=0;
	go(u){
		ll v=e[i].to;
		if(v==fa||ck[v]) continue;
		getroot(u,v);
		siz[u]+=siz[v];
		maxx=max(maxx,siz[v]);
	}
	maxx=max(maxx,nsiz-siz[u]);
	if(maxx<minn){
		minn=maxx;
		rt=u;
	}
}
void dfs1(ll fa,ll u){
	siz[u]=1;
	maxdep[u]=dep[u]=dep[fa]+1;
	tot[dep[u]]++;
	dson[u]=0;
	go(u){
		ll v=e[i].to;
		if(v==fa||ck[v]) continue;
		dfs1(u,v);
		if(maxdep[v]>maxdep[dson[u]]) dson[u]=v;
		maxdep[u]=max(maxdep[u],maxdep[v]);
		siz[u]+=siz[v];
	}
}
void dfs2(ll fa,ll u,ll tp){
	top[u]=tp;
	if(u==tp){
		vec[u].clear();
		vec[u].resize(maxdep[u]-dep[u]+1);
	}
	if(dson[u]) dfs2(u,dson[u],tp);
	go(u){
		ll v=e[i].to;
		if(v==fa||ck[v]) continue;
		if(v!=dson[u]) dfs2(u,v,v);
	}
}
void dfs3(ll fa,ll u){
	tot[dep[u]]++;
	tot1[0]+=siz[u]-1;
	tot2[0]+=siz[u]-1;
	go(u){
		ll v=e[i].to;
		if(v==fa||ck[v]) continue;
		dfs3(u,v);
	}
	go(u){
		ll v=e[i].to;
		if(v==fa||ck[v]||v==dson[u]) continue;
		son.push_back(v);
	}
	ll maxx=0;
	fr(i,0,(ll)son.size()-1){
		ll v=son[i];
		fr(j,0,(ll)vec[v].size()-1){
			ll now=vec[v][j];
			if((j+1)<(ll)vec[v].size()) now-=vec[v][j+1];
			tot1[max(0ll,j+1-dep[u]+1)]+=now*cntt[j+1];
			tot2[max(0ll,j+1-dep[u]+1)]+=now*cntt[j+2];
		}
		fr(j,0,(ll)vec[v].size()-1) cntt[j+1]+=vec[v][j];
		maxx=max(maxx,maxdep[v]-dep[u]);
	}
	ll tp=top[u];
	fr(i,1,maxx){
		ll now=vec[tp][dep[u]-dep[tp]+i];
		if((dep[u]-dep[tp]+i+1)<(ll)vec[tp].size()) now-=vec[tp][dep[u]-dep[tp]+i+1];
		tot1[max(0ll,i-dep[u]+1)]+=now*cntt[i];
		tot2[max(0ll,i-dep[u]+1)]+=now*cntt[i+1];
	}
	fr(i,1,maxx+1){
		if((dep[u]-dep[tp]+i)<(ll)vec[tp].size()) cntt[i]=vec[tp][dep[u]-dep[tp]+i];
		else cntt[i]=0;
	}
	pfr(i,(ll)son.size()-1,0){
		ll v=son[i];
		fr(j,0,(ll)vec[v].size()-1){
			ll now=vec[v][j];
			if((j+1)<(ll)vec[v].size()) now-=vec[v][j+1];
			tot1[max(0ll,j+1-dep[u]+1)]+=now*cntt[j+2];
			tot2[max(0ll,j+1-dep[u]+1)]+=now*cntt[j+2];
		}
		fr(j,0,(ll)vec[v].size()-1) cntt[j+1]+=vec[v][j];
	}
	fr(i,1,maxx+1) cntt[i]=0;
	fr(i,0,(ll)son.size()-1){
		ll v=son[i];
		fr(j,0,(ll)vec[v].size()-1) vec[tp][dep[u]-dep[tp]+j+1]+=vec[v][j];
	}
	vec[tp][dep[u]-dep[tp]]++;
	if(dson[u]) vec[tp][dep[u]-dep[tp]]+=vec[tp][dep[u]-dep[tp]+1];
	son.clear();
}
il void mdf(ll u,ll v){
	dfs2(u,v,v);
	dfs3(u,v);
	pfr(i,maxdep[v]-1,1) tot[i]+=tot[i+1];
	fr(i,1,maxdep[v]){
		sum1[i]-=tot[i];
		sum2[i]-=tot[i]*tot[i];
	}
	fr(i,1,maxdep[v]) ans-=(tot[i]-tot[i+1])*(sum1[i+1]*sum1[i+1]-sum2[i+1]);
	fr(i,0,maxdep[v]){
		if(!i) ans-=2*tot2[i]*(siz[u]-siz[v]);
		else{
			if(i>1) ans-=2*tot1[i]*(siz[u]-siz[v]-sum1[i-1]);
			ans-=2*tot2[i]*sum1[i];
		}
	}
	fr(i,1,maxdep[v]){
		sum1[i]+=tot[i];
		sum2[i]+=tot[i]*tot[i];
	}
	fr(i,0,maxdep[v]) tot[i]=tot1[i]=tot2[i]=0;
}
void solve(ll u){
	ck[u]=1;
	maxdep[u]=dep[u]=0;
	siz[u]=1;
	go(u){
		ll v=e[i].to;
		if(ck[v]) continue;
		dfs1(u,v);
		maxdep[u]=max(maxdep[u],maxdep[v]);
		siz[u]+=siz[v];
		pfr(j,maxdep[v]-1,1) tot[j]+=tot[j+1];
		fr(j,1,maxdep[v]){
			sum1[j]+=tot[j];
			sum2[j]+=tot[j]*tot[j];
		}
		fr(j,1,maxdep[v]) tot[j]=0;
	}
	ans-=sum1[1]*sum1[1]-sum2[1];
	go(u){
		ll v=e[i].to;
		if(ck[v]) continue;
		mdf(u,v);
	}
	fr(i,0,maxdep[u]) sum1[i]=sum2[i]=0;
	go(u){
		ll v=e[i].to;
		if(ck[v]) continue;
		minn=inf;
		nsiz=siz[v];
		getroot(u,v);
		solve(rt);
	}
}
il void clr(){
	fr(i,1,n) head[i]=ck[i]=0;
	cnt=ans=0;
}
int main(){
	t=read();
	while(t--){
		n=read();
		fr(i,2,n){
			ll u=read(),v=read();
			add(u,v);
			add(v,u);
		}
		ans=n*(n-1)*(n-2);
		minn=inf;
		nsiz=n;
		getroot(0,1);
		solve(rt);
		writeln(ans);
		clr();
	}
}
/*
2
3
1 2
2 3
4
1 2
2 3
2 4
ans:
4
18
*/

详细

Test #1:

score: 0
Memory Limit Exceeded

input:

3
1 2
2 3
4
1 2
2 3
2 4

output:


result: