102052024-03-29 13:39:09111Testvérvárosokcpp17Wrong answer 20/10071ms36024 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);
			auto q=z.find(x%K);
			if(q!=z.end())ans+=q->second;
			if(z.size()>s.size()){
				swap(s,z);
			}
			for(auto[y,c]:z){
				auto q=s.find(((x*2-y)%K+K)%K);
				if(q!=s.end())ans+=q->second;
			}
			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
2Accepted3ms2028 KiB
subtask20/15
3Accepted3ms2248 KiB
4Accepted3ms2448 KiB
5Wrong answer3ms2868 KiB
6Wrong answer3ms3112 KiB
7Wrong answer3ms3020 KiB
8Accepted3ms3288 KiB
9Wrong answer4ms3448 KiB
subtask30/15
10Wrong answer10ms5148 KiB
11Accepted4ms5380 KiB
12Wrong answer37ms11304 KiB
13Wrong answer32ms10784 KiB
14Wrong answer37ms11768 KiB
subtask420/20
15Accepted3ms4052 KiB
16Accepted3ms4564 KiB
17Accepted8ms7616 KiB
18Accepted21ms18272 KiB
19Accepted28ms24508 KiB
20Accepted39ms32428 KiB
21Accepted32ms30180 KiB
22Accepted48ms36024 KiB
subtask50/50
23Wrong answer64ms12248 KiB
24Wrong answer37ms8604 KiB
25Wrong answer46ms9520 KiB
26Wrong answer18ms5752 KiB
27Wrong answer35ms8876 KiB
28Wrong answer61ms12124 KiB
29Wrong answer67ms11732 KiB
30Wrong answer32ms11084 KiB
31Wrong answer71ms12900 KiB