Candies !

0

0 votes
Medium
Problem

Today is Mike's Birthday, so he wants to give candies to his classmates.

There are students in the class who are divided in groups. Each group members should get equal candies because they are close friends. 

But there are q students which want more candies than other particular student (jealousy), only then they will be happy. It is guaranteed that no two students of same group are jealous of each other.

Help Mike to find the minimum number of candies to distribute if there is any possible way that all students are happy.

It is guaranteed that each student belongs to one group only.

INPUT

The first line contains two integer n & m,  the number of students in the class and the number of groups in the class.

m lines follow, the ith of these lines contain single integer si denoting size of the ith group and sidistinct space separated  integers denoting group members , each between 1 to n.

Next line contains a single integer q denoting the number of students who are jealous.

q lines follow. Each of these lines contains two space separated integers u and v describes a relation between student u  to  vsuch that u wants atleast one more candy than v to be happy.(It is gauarnteed that u and v belongs to different groups).

OUTPUT

Print "POSSIBLE" if Mike can distribute candies such that all students are happy. In next line print  n space separated integers denoting the minimum number of candies which should be given to ith student.

Else print "IMPOSSIBLE".

CONSTRAINTS 

1m,sin2105

0qmin(106,n(n1))

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

As 1 from the first group hates 3 from the second group hence the members of the 1st group will get more candies than the 2nd group. So we can give 1 candy to group second students and 2 candies to first group students.

Editor Image

?