51132023-04-17 22:49:09nmarciTestvérvárosokcpp11Runtime error 80/100639ms522848 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];
int dp[50010][1010] = { 0 };
int 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);
            int 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
1Accepted4ms4224 KiB
2Accepted4ms5292 KiB
subtask215/15
3Accepted4ms5484 KiB
4Accepted4ms4960 KiB
5Accepted7ms10976 KiB
6Accepted13ms11240 KiB
7Accepted6ms11024 KiB
8Accepted8ms8720 KiB
9Accepted8ms14032 KiB
subtask315/15
10Accepted57ms109840 KiB
11Accepted21ms50740 KiB
12Accepted218ms394384 KiB
13Accepted166ms363952 KiB
14Accepted190ms411152 KiB
subtask40/20
15Accepted7ms13236 KiB
16Accepted17ms23784 KiB
17Accepted68ms106836 KiB
18Accepted300ms373840 KiB
19Runtime error239ms522848 KiB
20Runtime error250ms522828 KiB
21Runtime error202ms522796 KiB
22Runtime error209ms522768 KiB
subtask550/50
23Accepted483ms368488 KiB
24Accepted305ms211960 KiB
25Accepted305ms267960 KiB
26Accepted115ms81260 KiB
27Accepted238ms209796 KiB
28Accepted317ms398328 KiB
29Accepted639ms376940 KiB
30Accepted208ms380032 KiB
31Accepted560ms415220 KiB