104962024-04-03 15:03:39111A Day in Olbiacpp17Wrong answer 0/100667ms4992 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*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,0,-1,0);
	}
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
2Accepted3ms2060 KiB
subtask20/16
3Accepted4ms2284 KiB
4Accepted4ms2652 KiB
5Accepted4ms2956 KiB
6Wrong answer4ms3180 KiB
subtask30/16
7Accepted667ms3264 KiB
8Accepted648ms3336 KiB
9Accepted160ms3912 KiB
10Accepted232ms4076 KiB
11Accepted204ms4592 KiB
12Wrong answer172ms4720 KiB
13Wrong answer181ms4696 KiB
14Wrong answer175ms4912 KiB
15Wrong answer166ms4992 KiB
subtask40/16
16Runtime error3ms4368 KiB
17Runtime error2ms4364 KiB
subtask50/52
18Runtime error3ms4600 KiB
19Runtime error3ms4576 KiB
20Runtime error2ms4444 KiB
21Runtime error2ms4436 KiB
22Runtime error2ms4580 KiB
23Runtime error2ms4564 KiB
24Runtime error3ms4664 KiB
25Runtime error3ms4560 KiB
26Runtime error3ms4568 KiB
27Runtime error2ms4656 KiB
28Runtime error2ms4648 KiB
29Runtime error2ms4648 KiB
30Runtime error2ms4648 KiB
31Runtime error2ms4648 KiB