102022024-03-29 13:32:51111Testvérvárosokcpp17Time limit exceeded 15/1001.582s43696 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(x%K);
			if(z.size()>s.size()){
				swap(s,z);
			}
			for(int y:z){
				ans+=s.count(((x*2-y)%K+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
subtask215/15
3Accepted3ms2284 KiB
4Accepted3ms2440 KiB
5Accepted3ms2768 KiB
6Accepted3ms2768 KiB
7Accepted4ms2892 KiB
8Accepted3ms2852 KiB
9Accepted4ms3140 KiB
subtask30/15
10Accepted363ms6768 KiB
11Accepted24ms5580 KiB
12Time limit exceeded1.582s8432 KiB
13Time limit exceeded1.582s7468 KiB
14Time limit exceeded1.562s7844 KiB
subtask40/20
15Accepted3ms3508 KiB
16Accepted3ms4152 KiB
17Accepted8ms8216 KiB
18Accepted37ms21764 KiB
19Accepted82ms29640 KiB
20Accepted225ms39440 KiB
21Time limit exceeded1.577s18580 KiB
22Accepted97ms43696 KiB
subtask50/50
23Accepted100ms16096 KiB
24Accepted50ms10660 KiB
25Accepted68ms13364 KiB
26Accepted23ms6540 KiB
27Accepted50ms10536 KiB
28Accepted134ms17032 KiB
29Accepted96ms16232 KiB
30Time limit exceeded1.552s8700 KiB
31Accepted108ms17560 KiB