AtCoder Beginner Contest 002

Submission #1510720

Source codeソースコード

import java.util.*;
 
/**
 * http://abc002.contest.atcoder.jp/tasks/abc002_4
 */
public class Main {
 
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		boolean graph[][] = new boolean[N][N];
		for(int i=0; i<M; i++){
			int n1 = sc.nextInt();
			int n2 = sc.nextInt();
			graph[n1-1][n2-1] = true;
			graph[n2-1][n1-1] = true;
		}
		sc.close();
	
		System.out.println(maxClusterDfs(graph));	
	}

	public static int maxCluster(boolean[][] graph){
		int nodeSize = graph.length;
		
		int result = 0;
		for(int bmask=1; bmask < (1<<nodeSize); ++bmask){
			List<Integer> candidate = new ArrayList<>();
			int tmp = bmask;
			int shift=0;
			while(tmp != 0){
				if((tmp&1)!=0)candidate.add(shift);
				tmp>>=1;
				++shift;
			}
			if(isFullyConnected(graph, candidate))result=Math.max(result,candidate.size());
		}
		return result;
	}

	public static boolean isFullyConnected(boolean[][] graph, List<Integer>  nodeList){
		for(int i=0; i < nodeList.size(); ++i){
			for(int j=i+1; j < nodeList.size(); ++j){
				if(!graph[nodeList.get(i)][nodeList.get(j)])return false;
			}
		}
		return true;
	}
	
	public static int maxClusterDfs(boolean[][] graph){
		int nodeSize = graph.length;
		Set<Integer> visited = new HashSet<>();
		int result = 0;
		
		Map<Integer, Integer> memoization = new Map<Integer,Integer>();
		for(int start = 0; start < nodeSize; ++start){
			visited.add(start);
			result = Math.max(1 + dfs(graph, start, visited, memoization), result);
			visited.remove(start);
		}
		return result;
	}
	
	public static boolean isFullyConnected(boolean[][] graph, Set<Integer> visited, int newNode){
		for(int i : visited){
			if(!graph[i][newNode]) return false;
		}
		return true;
	}
	
	public static int dfs(boolean[][] graph, int currentNode, Set<Integer> visited, Map<Integer,Integer> memoization){
		int memoizationKey = (1 << currentNode) << graph.length;
		for(Integer v : visited) memoizationKey |= (1 << v);
		if(memoization.containsKey(memoizationKey)) return memoization.get(memoizationKey);
		int result = 0;
		for(int nextNode = 0; nextNode < graph.length; ++nextNode){
			if(!graph[currentNode][nextNode])continue;
			if(visited.contains(nextNode))continue;
			if(isFullyConnected(graph, visited, nextNode)){
				visited.add(nextNode);
				result = Math.max(result, 1 + dfs(graph, nextNode, visited, memoization));
				visited.remove(nextNode);
			}
		}
		memoization.put(memoizationKey, result);
		return result;
	}
}


Submission

Task問題 D - 派閥
User nameユーザ名 hi_viv
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 CE
Score得点 -
Source lengthソースコード長 2615 Byte
File nameファイル名
Exec time実行時間 -
Memory usageメモリ使用量 -

Compiler messageコンパイルメッセージ

./Main.java:57: error: Map is abstract; cannot be instantiated
Map<Integer, Integer> memoization = new Map<Integer,Integer>();
^
1 error

Test case