//Google Code Jam Round 1A 2008 B Milkshakes
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Google Code Jam
public class GoogleCode1 {
private static MyScanner in;
private static PrintStream out;
private static void solve() throws IOException
{
int C = in.nextInt();
for (int i = 0; i < C; i++) {
out.print("Case #" + (i + 1) + ": ");
solveCase();
}
}
static int M;
static HashMap<Integer, HashSet<Integer>> unmaltedPrefs;
static int[] maltedPrefs;
static HashSet<Integer> batchMalted;
static HashSet<Integer> batchUnmalted;
private static void solveCase() throws IOException {
unmaltedPrefs = new HashMap<>();
batchMalted = new HashSet<>();
batchUnmalted = new HashSet<>();
int N = in.nextInt();
M = in.nextInt();
maltedPrefs = new int[M];
for (int i = 0; i < M; i++) {
maltedPrefs[i] = -1;
}
for (int i = 0; i < M; i++) {
int T = in.nextInt();
HashSet<Integer> pref = new HashSet<>();
for (int j = 0; j < T; j++) {
int flav = in.nextInt();
int malted = in.nextInt();
if (malted == 1)
maltedPrefs[i] = flav;
else
pref.add(flav);
}
unmaltedPrefs.put(i, pref);
}
batchMalted = new HashSet<>();
batchUnmalted = new HashSet<>();
for (int i = 1; i <= N; i++) {
batchUnmalted.add(i);
}
boolean recheck = true;
while (recheck) {
recheck = false;
for (int i = 0; i < M; i++) {
if (checkCustSatisfied(i)) {
continue;
}
else {
int maltedPref = maltedPrefs[i];
if (maltedPref > 0) {
if (!batchMalted.contains(maltedPref)) {
batchMalted.add(maltedPref);
batchUnmalted.remove(maltedPref);
recheck = true;
}
}
}
}
}
boolean allSatisfied;
allSatisfied = true;
for (int i = 0; i < M; i++) {
if (!checkCustSatisfied(i)) {
allSatisfied = false;
break;
}
}
if (allSatisfied) {
String ans = "";
for (int i = 1; i <= N; i++) {
if (batchMalted.contains(i))
ans += "1 ";
else
ans += "0 ";
}
out.println(ans);
}
else
out.println("IMPOSSIBLE");
}
public static boolean checkCustSatisfied(int cust)
{
HashSet<Integer> pref = unmaltedPrefs.get(cust);
for (Integer p : pref) {
if (batchUnmalted.contains(p)) {
return true;
}
}
int maltedPref = maltedPrefs[cust];
if (maltedPref > 0 && batchMalted.contains(maltedPref))
return true;
return false;
}
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.txt");
}
else {
in = new MyScanner();
out = System.out;
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
No comments:
Post a Comment