Friendship value

1.8

5 votes
Graphs, Algorithms, Graph Representation
Problem

You want to place k people in a place shaped like a graph. The graph has n vertices and m edges.

The score of an arrangement is based on the following two factors:

  • Friendship is always important even in these tough times of Covid. Here, the ith person has friendship value fi. A connected component of people with the sum of friendship value equal to s adds s2 to score. A connected component is a set of people adjacent to each other. Two persons are adjacent if they are connected with an edge.
  • You also need to take care of the health of people. Here, the ith person has a danger value di. Two adjacent people i and j reduce the score by di×dj.

Your task is to print an arrangement with as high a score as possible. Note that you can drop a person and don't give him a vertice in the graph.

Input format

  • The first line contains k, n, and m.
  • The second line contains array f.
  • The third line contains array d.
  • The last n lines contain the table.

Output format

Print k integers. The ith number is the number of the vertex you assigned to the ith person. Otherwise, print 0 if you do not want to include this person in the arrangement.

Constraints

n=m=2000001k100001di,fi1000

Sample Input
4 3 1
1 2 3 4
5 6 7 8
1 3
Sample Output
2 3 0 1
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Note that this isn't necessarily the best arrangement. We assign the second vertex to the first person, the third vertex to the second person, and the first vertex to the fourth person. Also, we drop the third person. As the first vertex (where the fourth person is) is connected to the third vertex (where the second person is), they form a connected component and increase the score by (2+4)^2. The first person forms a single-member connected component and adds (1)^2 to the answer too. On the other hand, the second and the fourth person decrease the answer by 6*8. The overall score is 36+1-48=-11.

Editor Image

?