5114 | 2023-04-17 22:52:27 | nmarci | Testvérvárosok | cpp11 | Runtime error 30/100 | 648ms | 522528 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 4368 KiB | ||||
2 | Accepted | 4ms | 5776 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 4ms | 6400 KiB | ||||
4 | Accepted | 4ms | 5348 KiB | ||||
5 | Accepted | 7ms | 12792 KiB | ||||
6 | Accepted | 16ms | 15484 KiB | ||||
7 | Accepted | 7ms | 12124 KiB | ||||
8 | Accepted | 9ms | 11628 KiB | ||||
9 | Accepted | 8ms | 16224 KiB | ||||
subtask3 | 15/15 | ||||||
10 | Accepted | 52ms | 114336 KiB | ||||
11 | Accepted | 37ms | 73892 KiB | ||||
12 | Accepted | 232ms | 404256 KiB | ||||
13 | Accepted | 214ms | 374348 KiB | ||||
14 | Accepted | 202ms | 421696 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Accepted | 9ms | 18448 KiB | ||||
16 | Accepted | 27ms | 39152 KiB | ||||
17 | Accepted | 105ms | 199688 KiB | ||||
18 | Runtime error | 231ms | 522528 KiB | ||||
19 | Runtime error | 193ms | 522492 KiB | ||||
20 | Runtime error | 250ms | 522472 KiB | ||||
21 | Runtime error | 246ms | 522444 KiB | ||||
22 | Runtime error | 259ms | 522216 KiB | ||||
subtask5 | 0/50 | ||||||
23 | Runtime error | 568ms | 522156 KiB | ||||
24 | Accepted | 377ms | 341600 KiB | ||||
25 | Accepted | 382ms | 431964 KiB | ||||
26 | Accepted | 128ms | 116476 KiB | ||||
27 | Accepted | 307ms | 335996 KiB | ||||
28 | Accepted | 379ms | 499668 KiB | ||||
29 | Runtime error | 648ms | 521956 KiB | ||||
30 | Accepted | 222ms | 385276 KiB | ||||
31 | Runtime error | 531ms | 521444 KiB |