102042024-03-29 13:36:20111Testvérvárosokcpp17Wrong answer 20/10075ms38856 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,K;
	cin>>N>>K;
	vector<vector<pair<int,int>>>g(N+1);
	for(int i=0;i<N-1;i++){
		int a,b,d;
		cin>>a>>b>>d;
		g[a].emplace_back(b,d);
		g[b].emplace_back(a,d);
	}
	int ans=0;
	auto dfs=[&](auto self,int i,int p,int x)->map<int,int>{
		map<int,int>s;
		for(auto[j,d]:g[i]){
			if(j==p){
				continue;
			}
			auto z=self(self,j,i,x+d);
			int q=x%K;
			ans+=z.count(q)?z[q]:0;
			if(z.size()>s.size()){
				swap(s,z);
			}
			for(auto[y,c]:z){
				int q=((x*2-y)%K+K)%K;
				ans+=s.count(q)?s[q]:0;
			}
			for(auto[y,c]:z){
				s[y]+=c;
			}
		}
		s[x%K]++;
		return s;
	};
	dfs(dfs,1,0,0);
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2068 KiB
subtask20/15
3Accepted3ms2288 KiB
4Accepted3ms2320 KiB
5Wrong answer3ms2656 KiB
6Wrong answer3ms2768 KiB
7Wrong answer3ms2876 KiB
8Accepted3ms2904 KiB
9Wrong answer3ms3060 KiB
subtask30/15
10Wrong answer10ms4768 KiB
11Accepted4ms4860 KiB
12Wrong answer37ms10388 KiB
13Wrong answer32ms10096 KiB
14Wrong answer37ms10824 KiB
subtask420/20
15Accepted3ms3504 KiB
16Accepted3ms4256 KiB
17Accepted8ms7900 KiB
18Accepted25ms19668 KiB
19Accepted30ms26248 KiB
20Accepted39ms35088 KiB
21Accepted30ms32636 KiB
22Accepted52ms38856 KiB
subtask50/50
23Wrong answer68ms12208 KiB
24Wrong answer39ms8880 KiB
25Wrong answer48ms9524 KiB
26Wrong answer19ms5828 KiB
27Wrong answer37ms8880 KiB
28Wrong answer64ms12060 KiB
29Wrong answer71ms11780 KiB
30Wrong answer32ms11460 KiB
31Wrong answer75ms12924 KiB