10205 | 2024-03-29 13:39:09 | 111 | Testvérvárosok | cpp17 | Wrong answer 20/100 | 71ms | 36024 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 3ms | 2028 KiB | ||||
subtask2 | 0/15 | ||||||
3 | Accepted | 3ms | 2248 KiB | ||||
4 | Accepted | 3ms | 2448 KiB | ||||
5 | Wrong answer | 3ms | 2868 KiB | ||||
6 | Wrong answer | 3ms | 3112 KiB | ||||
7 | Wrong answer | 3ms | 3020 KiB | ||||
8 | Accepted | 3ms | 3288 KiB | ||||
9 | Wrong answer | 4ms | 3448 KiB | ||||
subtask3 | 0/15 | ||||||
10 | Wrong answer | 10ms | 5148 KiB | ||||
11 | Accepted | 4ms | 5380 KiB | ||||
12 | Wrong answer | 37ms | 11304 KiB | ||||
13 | Wrong answer | 32ms | 10784 KiB | ||||
14 | Wrong answer | 37ms | 11768 KiB | ||||
subtask4 | 20/20 | ||||||
15 | Accepted | 3ms | 4052 KiB | ||||
16 | Accepted | 3ms | 4564 KiB | ||||
17 | Accepted | 8ms | 7616 KiB | ||||
18 | Accepted | 21ms | 18272 KiB | ||||
19 | Accepted | 28ms | 24508 KiB | ||||
20 | Accepted | 39ms | 32428 KiB | ||||
21 | Accepted | 32ms | 30180 KiB | ||||
22 | Accepted | 48ms | 36024 KiB | ||||
subtask5 | 0/50 | ||||||
23 | Wrong answer | 64ms | 12248 KiB | ||||
24 | Wrong answer | 37ms | 8604 KiB | ||||
25 | Wrong answer | 46ms | 9520 KiB | ||||
26 | Wrong answer | 18ms | 5752 KiB | ||||
27 | Wrong answer | 35ms | 8876 KiB | ||||
28 | Wrong answer | 61ms | 12124 KiB | ||||
29 | Wrong answer | 67ms | 11732 KiB | ||||
30 | Wrong answer | 32ms | 11084 KiB | ||||
31 | Wrong answer | 71ms | 12900 KiB |