162752025-04-20 09:25:17HoraTestvérvárosokcpp17Wrong answer 0/100609ms174764 KiB
#include <bits/stdc++.h>
using namespace std;

vector<vector<array<int, 2>>> g;
int n, k, ans = 0;
vector<vector<int>> a;

void dfs(int v, int p){
    a[v][0] = 1;
    for(auto [x, w] : g[v]){
        if(x == p) continue;
        dfs(x, v);
        for(int i = 0; i < k; i++){
            ans += a[x][i] * a[v][(2 * k - i - w) % k];
        }
        for(int i = 0; i < k; i++){
            a[v][i] += a[x][(i + w) % k];
        }
    }
}

int main() {
    cin >> n >> k;
    g.resize(n + 1);
    a.resize(n + 1, vector<int>(k));
    for(int i = 0; i < n - 1; i++){
        int b, c, d;
        cin >> b >> c >> d;
        g[b].push_back({c, d});
        g[c].push_back({b, d});
    }
    dfs(1, 0);
    cout << ans;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Wrong answer1ms508 KiB
subtask20/15
3Wrong answer2ms564 KiB
4Wrong answer1ms316 KiB
5Wrong answer3ms780 KiB
6Wrong answer9ms2868 KiB
7Accepted2ms316 KiB
8Wrong answer6ms1940 KiB
9Wrong answer3ms912 KiB
subtask30/15
10Wrong answer17ms2012 KiB
11Accepted4ms820 KiB
12Wrong answer68ms6452 KiB
13Accepted59ms6128 KiB
14Wrong answer68ms6768 KiB
subtask40/20
15Wrong answer3ms820 KiB
16Wrong answer13ms4148 KiB
17Wrong answer41ms12340 KiB
18Wrong answer164ms45876 KiB
19Wrong answer123ms33128 KiB
20Wrong answer128ms28984 KiB
21Accepted52ms8664 KiB
22Wrong answer556ms174764 KiB
subtask50/50
23Wrong answer444ms129332 KiB
24Wrong answer256ms75828 KiB
25Wrong answer291ms76084 KiB
26Wrong answer108ms31028 KiB
27Wrong answer199ms51512 KiB
28Wrong answer189ms43572 KiB
29Wrong answer609ms172340 KiB
30Accepted68ms6196 KiB
31Wrong answer509ms150836 KiB