102012024-03-29 13:27:52111Testvérvárosokcpp17Wrong answer 0/1001.575s41996 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%K+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
1Accepted3ms1696 KiB
2Accepted3ms1880 KiB
subtask20/15
3Wrong answer3ms2088 KiB
4Accepted3ms2156 KiB
5Wrong answer4ms2528 KiB
6Accepted4ms2848 KiB
7Accepted4ms3032 KiB
8Accepted3ms2784 KiB
9Wrong answer4ms2988 KiB
subtask30/15
10Wrong answer363ms6248 KiB
11Accepted24ms5284 KiB
12Time limit exceeded1.575s8404 KiB
13Time limit exceeded1.559s7400 KiB
14Time limit exceeded1.567s7716 KiB
subtask40/20
15Wrong answer3ms3528 KiB
16Wrong answer3ms4100 KiB
17Wrong answer9ms7848 KiB
18Wrong answer39ms20668 KiB
19Wrong answer87ms27940 KiB
20Wrong answer230ms37496 KiB
21Time limit exceeded1.536s17708 KiB
22Wrong answer103ms41996 KiB
subtask50/50
23Wrong answer101ms15720 KiB
24Wrong answer50ms10268 KiB
25Wrong answer70ms13084 KiB
26Wrong answer23ms6632 KiB
27Wrong answer52ms10720 KiB
28Wrong answer136ms17168 KiB
29Wrong answer98ms16132 KiB
30Time limit exceeded1.567s8792 KiB
31Wrong answer115ms17688 KiB