91942024-02-17 21:59:27CWMTestvérvárosokcpp17Wrong answer 35/10059ms20648 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int main()
{
	int n, k;
	cin >> n >> k;
	vector<vector<pair<int,int>>> g(n);
	vector<int> modDist(n);
	vector<bool> visited(n);
	for (size_t i = 0; i < n-1; i++)
	{
		int a, b, w;
		cin >> a >> b >> w;
		a--; b--;
		g[a].push_back({ b,w });
		g[b].push_back({ a,w });
	}
	visited[0] = true;
	queue<int> BFS;
	BFS.push(0);
	while (!BFS.empty())
	{
		int node = BFS.front();
		BFS.pop();
		for (size_t i = 0; i < g[node].size(); i++)
		{
			if (!visited[g[node][i].first]) {
				visited[g[node][i].first] = true;
				modDist[g[node][i].first] = (modDist[node] + g[node][i].second)%k;
				BFS.push(g[node][i].first);
			}
		}
	}
	vector<int> kBuckets(k);
	for (size_t i = 0; i < n; i++)
	{
		kBuckets[modDist[i]]++;
	}
	long long res = 0;
	for (size_t i = 0; i < kBuckets.size(); i++)
	{
		int cnt = kBuckets[i];
		res += (cnt * (cnt - 1) / 2);
	}
	cout << res;
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Wrong answer3ms2068 KiB
subtask20/15
3Wrong answer3ms2276 KiB
4Accepted3ms2360 KiB
5Wrong answer3ms2784 KiB
6Wrong answer3ms3108 KiB
7Accepted3ms3324 KiB
8Wrong answer3ms3352 KiB
9Wrong answer4ms3700 KiB
subtask315/15
10Accepted16ms5020 KiB
11Accepted4ms4148 KiB
12Accepted57ms10864 KiB
13Accepted52ms10880 KiB
14Accepted59ms12488 KiB
subtask420/20
15Accepted3ms6212 KiB
16Accepted3ms6252 KiB
17Accepted8ms7164 KiB
18Accepted26ms9292 KiB
19Accepted35ms10848 KiB
20Accepted48ms13020 KiB
21Accepted45ms13244 KiB
22Accepted54ms15116 KiB
subtask50/50
23Wrong answer52ms15716 KiB
24Wrong answer30ms13560 KiB
25Wrong answer39ms15100 KiB
26Wrong answer12ms12736 KiB
27Wrong answer28ms14544 KiB
28Wrong answer57ms18320 KiB
29Wrong answer54ms18548 KiB
30Accepted54ms19236 KiB
31Wrong answer59ms20648 KiB