Wesley's Anger Contest 1 Problem 6 - Colourful Cactus Ordeal

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 2.5s
Java 4.5s
Memory limit: 256M
Java 512M

Author:
Problem types

You are given a cactus with N vertices and M edges. Recall that a cactus is a connected graph where every edge belongs to at most one cycle. Edge i connects vertices ui and vi. Your friend decided to colour the cactus with N different colours, numbered from 1 to N, with vertex i having colour ci. It may be the case that some colours are not present in the cactus. You are asked to determine for each colour, what is the length of the shortest path between any two distinct vertices of that colour? A path is a list of distinct vertices such that there is an edge between each pair of consecutive vertices in the list. The length of this path is the number of such edges.

Formally, let a path be a list of L vertices (P1,P2,,PL) such that there is an edge between vertices Pj and Pj+1 for all j (1jL1), and there does not exist a pair a,b where ab and Pa=Pb.

Constraints

For this problem, you will NOT be required to pass all the samples in order to receive points. In addition, all subtasks are disjoint, and you are NOT required to pass previous subtasks to earn points for a specific subtask.

Subtask Points N,M,K Additional Constraints
1 5% 1N1000
N1M1500
None
2 20% 1N100000
N1M150000
For each colour, there are at most 2 vertices of that colour
3 20% 1N100000
M=N1
None
4 55% 1N100000
N1M150000
None

For all subtasks:

1ui,viN

1ciN

The graph is a cactus.

There will be no self loops (an edge from a vertex to itself) and no multiple edges between any two vertices.

Input Specification

The first line contains 2 integers, N, and M.

The next line contains N integers, describing the colouring of the cactus. The ith integer contains the value ci, the colour of vertex i.

The next M lines describe the edges of the cactus. Each line contains 2 integers, ui and vi, indicating an unweighted bidirectional edge between ui and vi.

Output Specification

This problem is graded with an identical checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n character and that there are no trailing spaces.

Output a single line containing N space separated integers. The ith integer is the length of the shortest path between two distinct vertices of colour i. If there are less than two vertices of that colour, print -1 instead.

Sample Input 1

Copy
6 6
1 3 6 6 6 1
1 2
2 3
3 1
1 4
2 5
3 6

Sample Output 1

Copy
2 -1 -1 -1 -1 2

Sample Explanation 1

The shortest path between any two vertices of colour 1 (red) is 136.

The shortest path between any two vertices of colour 6 (green) is 325.

All other colours have less than two vertices.

Sample Input 2

Copy
5 4
2 5 4 5 4
4 2
5 2
4 1
3 4

Sample Output 2

Copy
-1 -1 -1 3 1

Sample Explanation 2

The shortest path between any two vertices of colour 4 (green) is 3425.

The shortest path between any two vertices of colour 5 (blue) is 24.

All other colours have less than two vertices.


Comments

There are no comments at the moment.