Maximum Weight

4.7

3 votes
C++, Graphs, Graph Representation, Algorithms
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Problem statement

Given an undirected graph of N nodes and M edges. Every node has a weight attached to it denoted by an array W. 

Select a subset of nodes say S, such that for every edge (u,v) in the above graph there exists either u or v in the subset S.

Let's define weight of set S as X|S|×W[i], where i denotes all the nodes present in the subset and |S| denotes size of subset.

Aim is to minimize X.

Note:

  • Calculation of X, will only include distinct elements from the subset, even if it contains duplicate elements.

Input format :

  • First line contains two space-separated integers : N and M 
  • Second line contain N space-separated integers denoting the array W.
  • Next M lines contains two space-separated integers u v, denoting an edge between node u and v.

Output format :

  • In the first line print an integer denoting the size of the second say Y (maximum value can be N).
  • In the next line print Y space separated integers denoting the nodes present in the subset.

Verdict and scoring :

  • Your score is equal to the sum of the values of all test files. The value of each test file is equal to the value of X that you found.

    If the subset is invalid you will receive a wrong answer verdict.

Constraints :

1N,M1051W[i]105

Sample Input
5 6
2 1 4 2 1
1 2
2 3
3 4
4 5
5 1
2 4
Sample Output
3
2 4 5
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • Subset {2, 4, 5} is a valid subset.
  • X = 3 * (W[2] + W[4] + W[5]) 
  • X = 3 *  4 = 12
Editor Image

?