#P17275. [eJOI 2026] Teamfulness

    ID: 19752 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>交互题Special JudgeeJOI(欧洲)2026

[eJOI 2026] Teamfulness

Problem Description

While walking around Kaunas during EJOI, Anton discovered NN spots where participants hang out. The spots are numbered from 00 to N−1N-1. Each spot is occupied by members of exactly one team, and teams are numbered from 00 to K−1K-1. Members of the same team may occupy any number of spots, and some teams may occupy no spots.

The spots are connected by N−1N-1 two-way roads such that there is exactly one simple path between any two spots; therefore, they form a tree. A simple path is a sequence of distinct spots in which every two consecutive spots are connected by a road. Its length is the number of roads it uses, one less than the number of spots it visits.

Anton wants to walk along a simple path and visit as many spots as possible. A simple path is interesting if its length is maximum among all simple paths in the tree. The teamfulness of a path is the number of distinct teams Anton encounters along it.

Find the sum of teamfulness over all different interesting paths. Two interesting paths are considered the same if and only if they visit exactly the same set of spots. In particular, traversing a path in the opposite direction does not create a different path.

Implementation details

Implement the following function:

long long teamfulness(int N, int K, std::vector<int> a,
                      std::vector<int> u, std::vector<int> v)
  • NN: the number of spots;
  • KK: the number of teams;
  • aa: an array of NN integers, where aia_i is the team occupying spot ii;
  • u,vu,v: arrays of N−1N-1 integers, where uiu_i and viv_i are the spots connected by the ii-th road.

The function is called exactly once per test and must return the sum of teamfulness over all interesting paths.

Input Format

Input format:

  • line 11: NN and KK;
  • line 22: NN integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1};
  • line 3+i3+i: two integers uiu_i and viv_i, the endpoints of the ii-th road.

Output Format

Output format:

  • line 11: the value returned by the function.
6 3
1 0 0 1 2 1
0 1
0 2
0 3
0 4
0 5
21
7 1
0 0 0 0 0 0 0
0 1
0 2
1 3
1 4
2 5
2 6
4
6 3
0 1 2 0 1 2
0 1
1 2
2 3
1 4
2 5
11

Hint

Explanation of example 1

The maximum length of a simple path is 22, so interesting paths have 22 roads and 33 spots. There are two interesting paths with teamfulness 33, seven with teamfulness 22, and one with teamfulness 11, giving a total of 2121.

Explanation of example 2

Team 00 is shown in yellow:

:::align{center} Tree for example 2 :::

Every path has teamfulness 11 because there is only one team. There are four interesting paths of length 44, so the sum is 44.

Explanation of example 3

Team 00 is yellow, team 11 is green, and team 22 is red:

:::align{center} Tree for example 3 :::

Interesting paths have length 33. There are four interesting paths: three have teamfulness 33, and one has teamfulness 22. Their total teamfulness is 1111.

Constraints

  • 3≤N≤1063\le N\le 10^6
  • 1≤K≤N1\le K\le N
  • 0≤ai<K0\le a_i<K for every 0≤i<N0\le i<N
  • 0≤ui,vi<N0\le u_i,v_i<N for every 0≤i<N−10\le i<N-1

Subtasks

Subtask Points NN KK Additional constraints
0 - The examples.
1 4 ≤106\le 10^6 ≤N\le N Every spot is directly connected to at most two other spots.
2 7 Some spot is directly connected to every other spot.
3 9 ≤200\le 200 -
4 10 ≤2⋅103\le 2\cdot 10^3
5 ≤106\le 10^6 =1=1
6 9 ≤2\le 2
7 11 ≤2⋅105\le 2\cdot 10^5 ≤50\le 50
8 12 ≤N\le N
9 13 ≤106\le 10^6 The length of an interesting path is odd.
10 15 -