BadHorse_图的着色转换


原题目在文章结尾

将一个集合划分为两个集合,且使得得到的每个集合都不存在内部“矛盾”。矛盾指同一集合的每两个元素不在输入中

方法:1:将不能位于同一集合的元素之间做一个连线,这样可以得到多个或单个图

           2:然后开始着色,相邻元素使用不同的颜色

           3: 最后判断,如果能全部使用两种颜色着色,则返回yes, 否则返回false

代码如下:

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;


public class BadHorse {
	
	private String inputFileString="";
	private String outputFileString="";
	private Integer num=0;
	private Map<Integer, Object>  map = new HashMap<Integer, Object>();
	private Map<Integer,Map<String,Integer> > tagMap = new HashMap<Integer,Map<String,Integer> > ();
	private Map<Integer,Boolean> outPutMap = new HashMap<Integer,Boolean>();
	
	BadHorse(String inputFileString , String outputFileString ){
		this.inputFileString = inputFileString;
		this.outputFileString = outputFileString;
	}
	
	// read into String[]
	private void Read() throws Exception{

		try{	
			BufferedReader tBufferedReader= new BufferedReader(new FileReader(inputFileString) );
			String sTempOneLine=  tBufferedReader.readLine();		
			num = Integer.parseInt(sTempOneLine);
			
			for(int i=1; i<= num ;i++){				
				Map<String, List<String>> mapCase = 
						new HashMap<String, List<String> > ();
				map.put(i, mapCase);
				Map<String, Integer> tagCase = 
						new HashMap<String, Integer > ();
				tagMap.put(i, tagCase);
				
				sTempOneLine = new String( tBufferedReader.readLine() );
				Integer count = Integer.parseInt(sTempOneLine);
				
				for(int j=0; j< count; j++){
					sTempOneLine = new String( tBufferedReader.readLine() );
					String[] strs = sTempOneLine.split(" ");
					PutIntoMap(i,strs);
				}				
			}		
			tBufferedReader.close();
		}
		catch(FileNotFoundException  e){
			e.printStackTrace();
			throw new FileNotFoundException();
		}
		catch (IOException io){			
			io.printStackTrace();
			throw new IOException();
		}
	}
	
	// put into  map[index]
	private void PutIntoMap(int index,String[] strs){
		
		String strA = strs[0];
		String strB = strs[1];
		Map<String, List<String>> mapCase= (Map<String, List<String>>) map.get(index);
		Map<String, Integer> tagCase =  tagMap.get(index);
		
		//strA
		List<String> sList=null;
		if(mapCase.containsKey(strA) == false){			
			sList = new ArrayList<String>();			
		}
		else sList = mapCase.get(strA);
		sList.add(strB);	
		mapCase.put(strA, sList);				
		tagCase.put(strA, 0);
		
		//strB
		if(mapCase.containsKey(strB) ==false){
			sList = new ArrayList<String>();				
		}
		else sList = mapCase.get(strB);
		sList.add(strA);
		mapCase.put(strB, sList);
		tagCase.put(strB, 0);
	
	}
	
	
	private void JudgeSet() throws IOException{
		Integer num = map.size();
		System.out.println("num =" +num);
		for(int i=1;i<=num;i++){
			Map<String, List<String>> mapCaseSet= (Map<String, List<String>>) map.get(i);
			
			
			Iterator<Map.Entry<String, List<String>> > mapIt=mapCaseSet.entrySet().iterator();			
			if(mapIt.hasNext()){
				Map.Entry<String, List<String>> mapEy = mapIt.next();
				String firstName = mapEy.getKey();
				
				Stack<String> ss = new Stack<String>();
				ss.push(firstName);
				Map<String, Integer> tagCaseSet =  tagMap.get(i);
				tagCaseSet.put(firstName, 2);
				
				if( oneCase(ss, i) )
					outPutMap.put(i, true);
				else 
					outPutMap.put(i, false);
			}
			
		}
	}
	
	private Boolean oneCase(Stack<String> ss, int i){
		
		Map<String, Integer> tagCaseSet =  tagMap.get(i);
		Map<String, List<String>> mapCaseSet= (Map<String, List<String>>) map.get(i);
		while(ss.isEmpty() == false ){
			String str = ss.pop();
			Integer tag = tagCaseSet.get(str);
			
			List<String> sList= mapCaseSet.get(str);				
			for(String sName: sList){
				
				Integer tagSName =tagCaseSet.get(sName);
				if( tagSName==0){							
					if(tag==1)
						tagCaseSet.put(sName, 2);
					else if(tag==2)
						tagCaseSet.put(sName, 1);
					ss.push(sName);
				}	
				else {
					if(tagSName ==tag) {
						
						return false;
					}								
				}						
			}
		}		
		return true;
	}
	
	private void outPutFile() throws IOException{
		
		try{			
			BufferedWriter bw = new BufferedWriter(new FileWriter(outputFileString));
			
			for(Map.Entry<Integer, Boolean> mapEy:outPutMap.entrySet()){
				String str = "Case #"+ String.valueOf(mapEy.getKey())+": ";
				str += mapEy.getValue()? "Yes\n":"No\n"; 
				bw.write(str);
			}
			bw.close();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
			throw new IOException(" write wrong!");
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		String inputFileString = "A-small-2-attempt0.in";
		String	outputFileString = "A-small-2-attempt0.out";
		
		BadHorse bh= new BadHorse(inputFileString,outputFileString);

		bh.Read();
		bh.JudgeSet();
		bh.outPutFile();
		
	}
}


 

原题如下:

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, T. T 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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值