Maximum weighted edges

3

4 votes
Graphs, Algorithms, Graph Representation
Problem

You are given a weighted array that contains N elements and a graph with N vertices and M edges. Determine a valid assignment of weights to vertices such that the value of E is maximum.

Here, E denotes the summation over all edges max (weight[u], weight[v]) and (u, v) belongs to M.

The assignment of weights must be valid by considering the following points:

  1. Every weight must be assigned to exactly one vertex.
  2. All weights must be assigned.

Input format

  • First line: Two integers denoting N, M
  • Next M lines: Two integers u, v denoting an edge between vertex u and vertex v

Output format

Print N elements where the ith element is the value of weight assigned to vertex i

Constraints

1N1e51M1e50Weight[]1e6

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Weight assignment is valid.

Edge (1,2) , max (weight[1],weight[2]) = 10
Edge (2,3) , max (weight[2],weight[3]) = 20
Edge (3,4) , max (weight[3],weight[4]) = 20
Edge (2,4) , max (weight[2],weight[4]) = 3

E = 10 + 20 + 20 + 3 = 53

Editor Image

?