Tuesday, December 24, 2013

Google Code Jam Notes - Bad Horse - Java

Problem

As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn't about to spend his valuable time figuring out how to split the League members by himself. That what he's got you -- his loyal henchman -- for.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a positive integer M on a line by itself -- the number of troublesome pairs of League members. The next M lines each contain a pair of names, separated by a single space.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "Yes" or "No", depending on whether the League members mentioned in the input can be split into two groups with neither of the groups containing a troublesome pair.

Limits

1 ≤ T ≤ 100.
Each member name will consist of only letters and the underscore character.
Names are case-sensitive.
No pair will appear more than once in the same test case.
Each pair will contain two distinct League members.

Small dataset

1 ≤ M ≤ 10.

Large dataset

1 ≤ M ≤ 100.

Sample


Input
 

Output
 
2
1
Dead_Bowie Fake_Thomas_Jefferson
3
Dead_Bowie Fake_Thomas_Jefferson
Fake_Thomas_Jefferson Fury_Leika
Fury_Leika Dead_Bowie
Case #1: Yes
Case #2: No
Analysis:
Solve this problem by using DFS, since large data set is 100, we can implement it recursively.

Time complexity: O(n^2)

My solution: (Your opinion is highly appreciated)
[java] view plaincopy
  1. package codeJam.google.com;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.util.HashSet;  
  8.   
  9. /** 
  10.  * @author Zhenyi 2013 Dec 21, 2013 11:37:20 AM 
  11.  */  
  12. public class BadHorse {  
  13.     static String[] st;  
  14.     static int M;  
  15.   
  16.     public static void main(String[] args) throws IOException {  
  17.         BufferedReader in = new BufferedReader(new FileReader(  
  18.                 "C:/Users/Zhenyi/Downloads/A-small-practice-1.in"));  
  19.         FileWriter out = new FileWriter(  
  20.                 "C:/Users/Zhenyi/Downloads/A-small-practice-1.out");  
  21.         // BufferedReader in = new BufferedReader(new  
  22.         // FileReader("C:/Users/Zhenyi/Downloads/A-small-practice-2.in"));  
  23.         // FileWriter out = new  
  24.         // FileWriter("C:/Users/Zhenyi/Downloads/A-small-practice-2.out");  
  25.   
  26.         int T = new Integer(in.readLine());  
  27.   
  28.         for (int cases = 1; cases <= T; cases++) {  
  29.             M = new Integer(in.readLine());  
  30.             boolean result = false;  
  31.             st = new String[M];  
  32.             for (int i = 0; i < M; i++) {  
  33.                 st[i] = in.readLine();  
  34.             }  
  35.   
  36.             HashSet<String> st1 = new HashSet<String>();  
  37.             HashSet<String> st2 = new HashSet<String>();  
  38.             result = travesal(M, st1, st2);  
  39.   
  40.             if (result) {  
  41.                 out.write("Case #" + cases + ": " + "Yes" + "\n");  
  42.             } else {  
  43.                 out.write("Case #" + cases + ": " + "No" + "\n");  
  44.             }  
  45.         }  
  46.         in.close();  
  47.         out.flush();  
  48.         out.close();  
  49.     }  
  50.   
  51.     private static boolean travesal(int m, HashSet<String> st1,  
  52.             HashSet<String> st2) {  
  53.         // TODO Auto-generated method stub  
  54.         if (m == 0) {  
  55.             return true;  
  56.         }  
  57.         int n = M - m;  
  58.         String[] s = st[n].split("\\s");  
  59.         if (!st1.contains(s[1]) && !st2.contains(s[0])) {  
  60.             int tmp1 = 0;  
  61.             int tmp2 = 0;  
  62.             if (st1.contains(s[0]))  
  63.                 tmp1 = 1;  
  64.             if (st2.contains(s[1]))  
  65.                 tmp2 = 1;  
  66.             st1.add(s[0]);  
  67.             st2.add(s[1]);  
  68.             if (travesal(m - 1, st1, st2)) {  
  69.                 return true;  
  70.             }  
  71.             if (tmp1 == 0) {  
  72.                 st1.remove(s[0]);  
  73.             }  
  74.             if (tmp2 == 0) {  
  75.                 st2.remove(s[1]);  
  76.             }  
  77.         }  
  78.   
  79.         if (!st1.contains(s[0]) && !st2.contains(s[1])) {  
  80.             int tmp1 = 0;  
  81.             int tmp2 = 0;  
  82.             if (st1.contains(s[1]))  
  83.                 tmp1 = 1;  
  84.             if (st2.contains(s[0]))  
  85.                 tmp2 = 1;  
  86.             st1.add(s[1]);  
  87.             st2.add(s[0]);  
  88.             if (travesal(m - 1, st1, st2)) {  
  89.                 return true;  
  90.             }  
  91.             if (tmp1 == 0) {  
  92.                 st1.remove(s[1]);  
  93.             }  
  94.             if (tmp2 == 0) {  
  95.                 st2.remove(s[0]);  
  96.             }  
  97.         }  
  98.         return false;  
  99.   
  100.     }  
  101. }  

6 comments:

  1. Hey, Can you please explain what the general idea is behind the problem. what is the fundamental condition that if checked in grouping?
    Thank you.

    ReplyDelete
  2. Hi Harsh,
    Sorry for the late reply, the ultimate goal of this problem is to divide each pair of bad horses to two different sets.
    Basically idea is to maintain two sets and recursively process each line, and check whether one of them could be put into one of the set. Since for the recursive call, each time we face two choices, thus we backtrack to the same node, we need to remember the states whether we need to remove the corresponding horse from the set. I use two helper variables tmp1 and tmp2 to remember the states.

    Let me know if this is not clear.

    Eric

    ReplyDelete
  3. An easier solution would be to create a graph of all the pairs troublesome pairs and then find for a cycle where the cycle length is odd.

    ReplyDelete
  4. I can't understand the dataset, like what is given in it. please can someone help me ?

    ReplyDelete
  5. whats the math logic behind splitting of the team

    ReplyDelete
  6. It's 2020 and I still can't figure what is expected in this problem... Probably it's a misunderstanding of what's being asked, but I can't solve a thing I can't understand :(

    ReplyDelete