104982024-04-03 15:29:46111A Day in Olbiacpp17Runtime error 32/100630ms4592 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

int get(map<int,int>&m,int x){
	auto t=m.find(x);
	return t==m.end()?0:t->second;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	if(N>1000)exit(1);
	vector<vector<int>>g(N);
	for(int i=0;i<N-1;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int ans=0;
	for(int m=1;m<=N;m++){
		auto dfs=[&](auto self,int i,int p,int d)->map<int,int>{
			map<int,int>s;
			for(int j:g[i]){
				if(j==p){
					continue;
				}
				auto z=self(self,j,i,d+1);
				ans+=get(z,-(i*m-d)-d*2);
				if(s.size()<z.size()){
					swap(s,z);
				}
				for(auto[x,y]:z){
					ans+=get(s,-x-d*2)*y;
				}
				for(auto[x,y]:z){
					s[x]+=y;
				}
			}
			s[i*m-d]++;
			return s;
		};
		dfs(dfs,8,-1,0);
	}
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Runtime error3ms1928 KiB
2Runtime error3ms2260 KiB
subtask216/16
3Accepted4ms2464 KiB
4Accepted4ms2732 KiB
5Accepted4ms2996 KiB
6Accepted4ms2916 KiB
subtask316/16
7Accepted620ms3160 KiB
8Accepted630ms3388 KiB
9Accepted160ms4092 KiB
10Accepted230ms4004 KiB
11Accepted202ms4212 KiB
12Accepted174ms4260 KiB
13Accepted187ms4376 KiB
14Accepted202ms4448 KiB
15Accepted165ms4592 KiB
subtask40/16
16Runtime error3ms3968 KiB
17Runtime error3ms3972 KiB
subtask50/52
18Runtime error2ms4036 KiB
19Runtime error3ms4256 KiB
20Runtime error2ms4264 KiB
21Runtime error3ms4336 KiB
22Runtime error3ms4352 KiB
23Runtime error3ms4352 KiB
24Runtime error3ms4368 KiB
25Runtime error2ms4468 KiB
26Runtime error3ms4468 KiB
27Runtime error3ms4460 KiB
28Runtime error2ms4356 KiB
29Runtime error2ms4356 KiB
30Runtime error2ms4368 KiB
31Runtime error3ms4484 KiB