Graph and Primes

4.7

3 votes
Graph, Shortest path problem, Prime Factorization, Dijkstra's algorithm, Algorithms, Very-Easy
Problem

You have been given a graph G consisting of n nodes and m edges. Also, you have been given a multi-set S of size m consisting of integers.

Now, you need to assign a single integer from S as weight to each edge of the graph, such that the weight of the shortest path among node 1 and maximum number of other nodes is a prime number. Let the number of nodes having weight of shortest path among node 1 and itself a prime number be k. You need to try and maximize k

Each element of set S has to be used exactly once, a single integer from the Set being assigned to a single edge of the given graph. 

Input Format :

The first line contains two integers n and m.

Each of the next m lnes contains two space separated integers (u,v) indicating an edge between nodes u and v in graph G.

The next line contains m space separated integers , where the ith integer indicates the ith element of the given set.

It is guaranteed there will be no self loops and multiple edges, and the given input graph will be connected. 

Output Format :

Print m lines, on the ith line print a single integer wi denoting the weight assigned to the ithedge given in the input in the same order.

Note that the union of all wi should be equal to the set S.

Test Generation Process:

First, a random tree consisting of n nodes is generated using the following pseudo-code :

for (i = 2 to n)
{
   parent[i] = rnd(1, i - 1)

   addEdge(i, parent[i])
}

Next,

while (num_edges < m)
{
    x = rnd(1, n), y = rnd(1, n)

    if (x != y and edge(x, y) is new)
    {
       addEdge(x, y);
    }
}

 

The elements of the set S are generated randomly , each number with equal probability. The function rnd(x,y)  generates a random integer equi-probably in the range [x,y]

Scoring :

There are 5 input files

For the first test file :

n=100,m=700,maxxSx=20

For the next two test files , 

n=1000,m=10000,maxxSx=200

For the next two test files , 

n=5000,m=100000,maxxSx=1000

Additionally, 5 more test files will be added and all submissions will be rejudged with the following constraints :

n=1000,m=999,maxxSx=200

n=1000,m=20000,maxxSx=200

n=5000,m=4999,maxxSx=1000

n=5000,m=1 90000,maxxSx=1000

n=5000,m=2 00000,maxxSx=1000

Let the number of nodes having weight of shortest path between it and node 1 be k in any test file. Your score for that test will be :

kn110. Your overall score will be the sum of scores over all test files. 

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

There are many possible assignments of weights to edges, one of them being the order [3,2,1] For this, nodes 2 ,3 have weight of shortest path being prime numbers. So, your score that would be calculated is :

2310=6.6666666....

Editor Image

?