13023번: ABCDE

문제의 조건에 맞는 A, B, C, D, E가 존재하면 1을 없으면 0을 출력한다.

www.acmicpc.net

 

 

 

 


 

 

 

 

  • 풀이

 

처음에 문제를 읽고 이해가 되지않아서 해맸습니다.

 

정리하자면, 모든 노드 중 ABCDE의 예시처럼 5개의 노드가 예시의 입력이 주어지는 가를 맞추는 문제입니다.

즉, 모든 노드를 확인하며 ABCDE처럼 5개의 노드까지가면 1출력, 모든 노드가 ABCDE처럼 5개의 노드를 갈 수 없다면 0출력해주면 됩니다.

 

저는 ArrayList 배열을 통해 노드들을 연결했고, boolean 배열을 통해 방문을 체크해주었습니다.

돌리는 방식은 dfs방식으로 depth가 5이면 1을 출력하도록 했습니다.

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
	static ArrayList[] array;
	static boolean[] check;
	static int N, M, answer;
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SetData();
		System.out.println(answer);
	}
	
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);
		
		N = in.nextInt();
		M = in.nextInt();
		answer = 0;
		array = new ArrayList[N];
		check = new boolean[N];
		for(int i = 0 ; i < N; i++)
			array[i] = new ArrayList<Integer>();
		
		
		for(int i = 0 ; i < M; i++) {
			int a = in.nextInt();
			int b = in.nextInt();
			
			array[a].add(b);
			array[b].add(a);
		}
		
		for(int i = 0 ; i < N; i++) {
			if(answer == 1) break;
			
			check[i] = true;
			dfs(i, 1);
			check[i] = false;
		}
	}
	
	private static void dfs(int index, int depth) {
		if(depth == 5) { 
			answer = 1;
			return;
		}
		
		for(int i = 0; i < array[index].size(); i++) {
			int temp = (int) array[index].get(i);
			if(check[temp]) continue;
			
			check[temp] = true;
			dfs(temp, depth+1);
			check[temp] = false;
		}
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts