Opps, why can not input chinese?
Tips: the point is "Each customer will like at most one malted flavor"
Copy main method, will resort tomorrow.
Tips: the point is "Each customer will like at most one malted flavor"
Copy main method, will resort tomorrow.
1
2 public static void main(String[] args) {
3 try {
4 BufferedReader br = new BufferedReader(new InputStreamReader("".getClass().getResourceAsStream("/B-small-practice.in")));
5 int testCaseNumber = new Integer(br.readLine());
6 for(int i = 0; i < testCaseNumber; i++) {
7 int milkshakeFlavorsNumber = new Integer(br.readLine());
8 int customerNumber = new Integer(br.readLine());
9
10 int[] milkshakeFlavors = new int[milkshakeFlavorsNumber];
11 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
12 milkshakeFlavors[j] = 0;
13 }
14 List<Integer> maltedFlavor = new ArrayList<Integer>();
15 List<List<Integer>> customerFlavors = new ArrayList<List<Integer>>();
16 for(int j = 0; j < customerNumber; j++) {
17 int[] customer = parseToIntArray(br.readLine().split(" "));
18 List<Integer> flavors = new ArrayList<Integer>();
19 if(customer[0] == 1 && customer[2] == 1) {
20 if(!maltedFlavor.contains(new Integer(customer[1] - 1)))
21 maltedFlavor.add(customer[1] - 1);
22 } else {
23 for(int k = 0; k < customer[0]; k++) {
24 int flavor = customer[2 * k + 1];
25 int malted = customer[2 * k + 2];
26 flavors.add(flavor - 1);
27 flavors.add(malted);
28 }
29 customerFlavors.add(flavors);
30 }
31 }
32 if(maltedFlavor.size() == 0) {
33 System.out.print("Case #" + (i + 1) +": ");
34 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
35 System.out.print("0 ");
36 }
37 System.out.println();
38 } else {
39 boolean impossible = false;
40 do{
41 int maltedFlavorIndex = maltedFlavor.get(0);
42 milkshakeFlavors[maltedFlavorIndex] = 1;
43 maltedFlavor.remove(0);
44 for(int j = 0; j < customerFlavors.size(); j++) {
45 for(int k = 0; k < customerFlavors.get(j).size(); k += 2) {
46 if(customerFlavors.get(j).get(k).equals(maltedFlavorIndex)
47 && customerFlavors.get(j).get(k + 1) == 0) {
48 customerFlavors.get(j).remove(k + 1);
49 customerFlavors.get(j).remove(k);
50 }
51 }
52 if(customerFlavors.get(j).size() == 0) {
53 impossible = true;
54 break;
55 } else if (customerFlavors.get(j).size() == 2
56 && customerFlavors.get(j).get(1) == 1) {
57 if(milkshakeFlavors[customerFlavors.get(j).get(0)] == 0)
58 maltedFlavor.add(customerFlavors.get(j).get(0));
59 }
60 }
61 if(impossible) {
62 break;
63 }
64 } while (maltedFlavor.size() > 0);
65 if (impossible) {
66 System.out.println("Case #" + (i + 1) + ": IMPOSSIBLE");
67 } else {
68 System.out.print("Case #" + (i + 1) +": ");
69 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
70 System.out.print(milkshakeFlavors[j] + " ");
71 }
72 System.out.println();
73 }
74 }
75 }
76 } catch(Exception e) {
77 e.printStackTrace();
78 }
79 }
2 public static void main(String[] args) {
3 try {
4 BufferedReader br = new BufferedReader(new InputStreamReader("".getClass().getResourceAsStream("/B-small-practice.in")));
5 int testCaseNumber = new Integer(br.readLine());
6 for(int i = 0; i < testCaseNumber; i++) {
7 int milkshakeFlavorsNumber = new Integer(br.readLine());
8 int customerNumber = new Integer(br.readLine());
9
10 int[] milkshakeFlavors = new int[milkshakeFlavorsNumber];
11 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
12 milkshakeFlavors[j] = 0;
13 }
14 List<Integer> maltedFlavor = new ArrayList<Integer>();
15 List<List<Integer>> customerFlavors = new ArrayList<List<Integer>>();
16 for(int j = 0; j < customerNumber; j++) {
17 int[] customer = parseToIntArray(br.readLine().split(" "));
18 List<Integer> flavors = new ArrayList<Integer>();
19 if(customer[0] == 1 && customer[2] == 1) {
20 if(!maltedFlavor.contains(new Integer(customer[1] - 1)))
21 maltedFlavor.add(customer[1] - 1);
22 } else {
23 for(int k = 0; k < customer[0]; k++) {
24 int flavor = customer[2 * k + 1];
25 int malted = customer[2 * k + 2];
26 flavors.add(flavor - 1);
27 flavors.add(malted);
28 }
29 customerFlavors.add(flavors);
30 }
31 }
32 if(maltedFlavor.size() == 0) {
33 System.out.print("Case #" + (i + 1) +": ");
34 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
35 System.out.print("0 ");
36 }
37 System.out.println();
38 } else {
39 boolean impossible = false;
40 do{
41 int maltedFlavorIndex = maltedFlavor.get(0);
42 milkshakeFlavors[maltedFlavorIndex] = 1;
43 maltedFlavor.remove(0);
44 for(int j = 0; j < customerFlavors.size(); j++) {
45 for(int k = 0; k < customerFlavors.get(j).size(); k += 2) {
46 if(customerFlavors.get(j).get(k).equals(maltedFlavorIndex)
47 && customerFlavors.get(j).get(k + 1) == 0) {
48 customerFlavors.get(j).remove(k + 1);
49 customerFlavors.get(j).remove(k);
50 }
51 }
52 if(customerFlavors.get(j).size() == 0) {
53 impossible = true;
54 break;
55 } else if (customerFlavors.get(j).size() == 2
56 && customerFlavors.get(j).get(1) == 1) {
57 if(milkshakeFlavors[customerFlavors.get(j).get(0)] == 0)
58 maltedFlavor.add(customerFlavors.get(j).get(0));
59 }
60 }
61 if(impossible) {
62 break;
63 }
64 } while (maltedFlavor.size() > 0);
65 if (impossible) {
66 System.out.println("Case #" + (i + 1) + ": IMPOSSIBLE");
67 } else {
68 System.out.print("Case #" + (i + 1) +": ");
69 for(int j = 0; j < milkshakeFlavorsNumber; j++) {
70 System.out.print(milkshakeFlavors[j] + " ");
71 }
72 System.out.println();
73 }
74 }
75 }
76 } catch(Exception e) {
77 e.printStackTrace();
78 }
79 }