51142023-04-17 22:52:27nmarciTestvérvárosokcpp11Runtime error 30/100648ms522528 KiB
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <deque>
#include <queue>
#include <algorithm>

using namespace std;
using ll = long long int;
int k;
vector<pair<int,int>> g[50010];
ll dp[50010][1010] = { 0 };
ll sol = 0;        //lehet overflow

void dfs(int node, int prev = -1) {
    dp[node][0] = 1;
    for (auto i : g[node]) {
        if (i.first != prev) {
            dfs(i.first, node);
            ll mock[1010] = { 0 };
            for (int j = 0; j < k; ++j) {
                int kellmeg = (k - ((j + i.second) % k)) % k;
                //cerr << ((j + i.second) % k) << " " << kellmeg << endl;
               // cerr << node << endl;
               // cerr << j << " " << i.second << " " << kellmeg << " " << dp[node][kellmeg] * dp[i.first][j] << endl;
                sol += dp[node][kellmeg] * dp[i.first][j];
                mock[(j + i.second) % k] += dp[i.first][j];
            }
            for (int j = 0; j < k; ++j) {
                dp[node][j] += mock[j];
            }
        }
    }
}

int main()
{
    int n;
    cin >> n >> k;
    for (int i = 0; i < n - 1; ++i) {
        int a, b, t;
        cin >> a >> b >> t;
        g[a].emplace_back(b, t);
        g[b].emplace_back(a, t);
    }
    //cerr << endl << endl;
    dfs(1);
    cout << sol << endl;
    /*for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < k; ++j) {
            cerr << dp[i][j] << " ";
        }
        cerr << endl;
    }*/
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms4368 KiB
2Accepted4ms5776 KiB
subtask215/15
3Accepted4ms6400 KiB
4Accepted4ms5348 KiB
5Accepted7ms12792 KiB
6Accepted16ms15484 KiB
7Accepted7ms12124 KiB
8Accepted9ms11628 KiB
9Accepted8ms16224 KiB
subtask315/15
10Accepted52ms114336 KiB
11Accepted37ms73892 KiB
12Accepted232ms404256 KiB
13Accepted214ms374348 KiB
14Accepted202ms421696 KiB
subtask40/20
15Accepted9ms18448 KiB
16Accepted27ms39152 KiB
17Accepted105ms199688 KiB
18Runtime error231ms522528 KiB
19Runtime error193ms522492 KiB
20Runtime error250ms522472 KiB
21Runtime error246ms522444 KiB
22Runtime error259ms522216 KiB
subtask50/50
23Runtime error568ms522156 KiB
24Accepted377ms341600 KiB
25Accepted382ms431964 KiB
26Accepted128ms116476 KiB
27Accepted307ms335996 KiB
28Accepted379ms499668 KiB
29Runtime error648ms521956 KiB
30Accepted222ms385276 KiB
31Runtime error531ms521444 KiB