参照博客http://blog.csdn.net/badmartin/article/details/11681627

实现了java版本的算法如下

package com.google;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
/**
 * 黑马
 */
public class BadHorse {
    public static String Input_File_Name = "A-small-2-attempt0.in";
    public static String Output_File_Name = "A-small-1-attempt0.out";
    public static int MaxSize = 1000;
    // 重定向输入输出
    static {
        try {
            /*PrintStream ps = new PrintStream(new FileOutputStream(Output_File_Name));
            System.setOut(ps);*/
            FileInputStream fis = new FileInputStream(Input_File_Name);
            System.setIn(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 判断一个图是否是二分部
    public static boolean judgeBiGraph(int start, Node[] graph) {
          
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.offer(start); //添加到队列
        graph[start].setColor(0);
          
        while(!queue.isEmpty()){
            int id = queue.poll();//取出队列头元素,并移除元素
            List<Integer> neigh_list = graph[id].getNeigh_list();
            int color = graph[id].getColor();
            for (Iterator<Integer> iterator = neigh_list.iterator(); iterator.hasNext();) {
                Integer nid = (Integer) iterator.next();
                if(graph[nid].getColor()==-1){
                    graph[nid].setColor((color+1)%2);
                    queue.offer(nid);
                }else{
                    if(color == graph[nid].getColor()){
                        return false;
                    }
                }
            }
        }
        return true;
    }
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
        int T = 0;
        int caseid = 0;
        int M = 0;
        if(sc.hasNextLine()){
            T = Integer.parseInt(sc.nextLine());
        }
          
        while(caseid < T){ //循环遍历所有的case
            Map<String,Integer> map = new HashMap<String,Integer>();
            if(sc.hasNextLine())
                M = Integer.parseInt(sc.nextLine());
            Node[] graph = new Node[MaxSize];
            for(int j=0;j<MaxSize;j++){
                graph[j] = new Node();
            }
            String s = "";
            String s1 = "";
            String s2 = "";
            int id = 0;
            for(int i=0;i<M;i++){ //循环每行数据,构造图,采用邻接表表示图
                if(sc.hasNextLine())
                    s = sc.nextLine();
                String[] temp = s.split(" ");
                s1 = temp[0];
                s2 = temp[1];
                int id1;
                int id2;
                if(map.get(s1)==null){
                    map.put(s1, id);
                    id1 = id;
                    id++;
                }else{
                    id1 = map.get(s1);
                }
                if(map.get(s2)==null){
                    map.put(s2, id);
                    id2 = id;
                    id++;
                }else{
                    id2 = map.get(s2);
                }
                graph[id1].getNeigh_list().add(id2);
                graph[id2].getNeigh_list().add(id1);
            }
            if(BadHorse.judgeBiGraph(0,graph)){
                System.out.println("Case #"+(caseid+1)+": Yes");
            }else{
                System.out.println("Case #"+(caseid+1)+": No");
            }
            caseid++;
        }
    }
}
// 图中节点定义
class Node {
    private int color;
    private List<Integer> neigh_list;
    public Node() {
        this.color = -1;
        this.neigh_list = new ArrayList<Integer>();
    }
    public int getColor() {
        return color;
    }
    public void setColor(int color) {
        this.color = color;
    }
    public List<Integer> getNeigh_list() {
        return neigh_list;
    }
    public void setNeigh_list(List<Integer> neigh_list) {
        this.neigh_list = neigh_list;
    }
}