102002024-03-29 13:21:24111Testvérvárosokcpp17Wrong answer 0/1001.6s47908 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)->multiset<int>{
		multiset<int>s;
		for(auto[j,d]:g[i]){
			if(j==p){
				continue;
			}
			auto z=self(self,j,i,x+d);
			ans+=z.count((K-x%K)%K);
			if(z.size()>s.size()){
				swap(s,z);
			}
			for(int y:z){
				ans+=s.count((K-(y-x)%K)%K);
			}
			s.insert(z.begin(),z.end());
		}
		s.insert(x%K);
		return s;
	};
	dfs(dfs,1,0,0);
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2072 KiB
subtask20/15
3Wrong answer3ms2284 KiB
4Accepted3ms2492 KiB
5Wrong answer4ms3080 KiB
6Accepted3ms3104 KiB
7Accepted4ms3260 KiB
8Accepted3ms3304 KiB
9Wrong answer4ms3820 KiB
subtask30/15
10Wrong answer361ms7012 KiB
11Accepted24ms6160 KiB
12Time limit exceeded1.6s9712 KiB
13Time limit exceeded1.559s9404 KiB
14Time limit exceeded1.574s10620 KiB
subtask40/20
15Wrong answer3ms6252 KiB
16Wrong answer4ms6764 KiB
17Wrong answer9ms10844 KiB
18Wrong answer39ms23972 KiB
19Wrong answer86ms31924 KiB
20Wrong answer226ms42112 KiB
21Time limit exceeded1.582s22884 KiB
22Wrong answer101ms47908 KiB
subtask50/50
23Wrong answer98ms22152 KiB
24Wrong answer50ms17372 KiB
25Wrong answer68ms20556 KiB
26Wrong answer23ms13960 KiB
27Wrong answer50ms18564 KiB
28Wrong answer133ms25632 KiB
29Wrong answer101ms25572 KiB
30Time limit exceeded1.547s19008 KiB
31Wrong answer108ms28688 KiB