Skip to content

Conversation

@LiiNi-coder
Copy link
Contributor

🧷 문제 링크

https://www.acmicpc.net/submit/16168/100549762

🧭 풀이 시간

120 분

👀 체감 난이도

✏️ 문제 설명

  • 그래프에 대해서, 모든 지점을 지나면서 모든 연결 구간들을 지나고 싶음. 하지만 같은 연결 구간을 두 번 이상 지나지 않도록 하기. 이때 그 경로가 존재하는지 출력

🔍 풀이 방법

  • dfs + 유러시안 패스 경로

⏳ 회고

  • 유러시안 패스 경로 식 : degree가 홀수인 정점의 개수가 0또는2
  • 그래프의 단일 컴포넌트 확인 절차 -> dfs로 하였을땐 visited[] 로 들른 정점의 개수 카운트
  • 처음엔 유러시안 패스 경로식을 안쓰고 뻘짓을 이렇게 하느라 시간이 오래 걸림. 아래 코드 결과 시간초과가 나옴.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main{
	private static int V;
	private static int E;
	private static List<List<Integer>> Graph;
	private static List<Set<Integer>> Visited;
	public static void main(String[] args) throws IOException {
		String[] temp;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		temp = br.readLine().split(" ");
		V = Integer.parseInt(temp[0]);
		E = Integer.parseInt(temp[1]);
		Graph = new ArrayList<>();
		for(int i = 0; i < V+1; i++){
			Graph.add(new ArrayList<>());
		}
		for(int i = 0; i < E; i++){
			temp = br.readLine().split(" ");
			int s = Integer.parseInt(temp[0]);
			int e = Integer.parseInt(temp[1]);
			Graph.get(s).add(e);
			Graph.get(e).add(s);
		}

		boolean isAnswer = false;
		for(int i = 1; i <= V; i++)
			if(dfs(i)){
				isAnswer = true;
				break;
			}

		System.out.println(isAnswer? "YES" : "NO");
		br.close();
	}

	private static boolean dfs(int sv) {
		Visited = new ArrayList<>();
		for(int i = 0; i < V+1; i++){
			Visited.add(new HashSet<>());
		}

		recur(sv);
		return false;
	}

	private static void recur(int sv) {
		int passedEdges = 0;
		for(var nvSet: Visited){
			passedEdges += nvSet.size();
		}
		if(passedEdges == E){
			System.out.println("YES");
			System.exit(0);
		}

		List<Integer> nvs = Graph.get(sv);
		for(int nv : nvs){
			if(haveBeenGoneEdge(sv, nv))
				continue;
			int min = Math.min(sv, nv);
			int max = Math.max(sv, nv);
			Visited.get(min).add(max);
			recur(nv);
			Visited.get(min).remove(max);
		}
	}

	private static boolean haveBeenGoneEdge(int sv, int nv) {
		int min = Math.min(sv, nv);
		int max = Math.max(sv, nv);
		var temp = Visited.get(min);
		if(temp == null)
			return false;
		return temp.contains(max);
	}
}

@LiiNi-coder LiiNi-coder added the fail 😢 해설을 보고 풀었거나, 못 풀었을 때 label Nov 26, 2025
@ShinHeeEul ShinHeeEul merged commit 932e41e into main Nov 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fail 😢 해설을 보고 풀었거나, 못 풀었을 때

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants