5113 | 2023-04-17 22:49:09 | nmarci | Testvérvárosok | cpp11 | Futási hiba 80/100 | 639ms | 522848 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;
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 4ms | 4224 KiB | ||||
2 | Elfogadva | 4ms | 5292 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Elfogadva | 4ms | 5484 KiB | ||||
4 | Elfogadva | 4ms | 4960 KiB | ||||
5 | Elfogadva | 7ms | 10976 KiB | ||||
6 | Elfogadva | 13ms | 11240 KiB | ||||
7 | Elfogadva | 6ms | 11024 KiB | ||||
8 | Elfogadva | 8ms | 8720 KiB | ||||
9 | Elfogadva | 8ms | 14032 KiB | ||||
subtask3 | 15/15 | ||||||
10 | Elfogadva | 57ms | 109840 KiB | ||||
11 | Elfogadva | 21ms | 50740 KiB | ||||
12 | Elfogadva | 218ms | 394384 KiB | ||||
13 | Elfogadva | 166ms | 363952 KiB | ||||
14 | Elfogadva | 190ms | 411152 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Elfogadva | 7ms | 13236 KiB | ||||
16 | Elfogadva | 17ms | 23784 KiB | ||||
17 | Elfogadva | 68ms | 106836 KiB | ||||
18 | Elfogadva | 300ms | 373840 KiB | ||||
19 | Futási hiba | 239ms | 522848 KiB | ||||
20 | Futási hiba | 250ms | 522828 KiB | ||||
21 | Futási hiba | 202ms | 522796 KiB | ||||
22 | Futási hiba | 209ms | 522768 KiB | ||||
subtask5 | 50/50 | ||||||
23 | Elfogadva | 483ms | 368488 KiB | ||||
24 | Elfogadva | 305ms | 211960 KiB | ||||
25 | Elfogadva | 305ms | 267960 KiB | ||||
26 | Elfogadva | 115ms | 81260 KiB | ||||
27 | Elfogadva | 238ms | 209796 KiB | ||||
28 | Elfogadva | 317ms | 398328 KiB | ||||
29 | Elfogadva | 639ms | 376940 KiB | ||||
30 | Elfogadva | 208ms | 380032 KiB | ||||
31 | Elfogadva | 560ms | 415220 KiB |