๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 14217_๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

๋ฟŒ์•ผ._. 2023. 5. 26. 13:23

Gold V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/14217)

< ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ >

 

๋ฌธ์ œ ํ’€์ด

 

1. ๊ฐ ๋„์‹œ -> ์ˆ˜๋„๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

: ์‹œ๊ฐ„์ดˆ๊ณผ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

2. ์ˆ˜๋„ -> ๊ฐ ๋„์‹œ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

: bfs๋ฅผ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ต์„ ์–ป์„ ์ˆ˜ ์žˆ์–ด 1๋ฒˆ๋ณด๋‹ค ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ• ๋ง๊ณ ๋Š” ๋‹จ์ˆœ bfs๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;


public class Main {
	static ArrayList<ArrayList<Integer>> arr;
	static boolean visited[];
	static int result[];

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		
		int n=Integer.parseInt(st.nextToken());
		int m=Integer.parseInt(st.nextToken());
		
		// init
		arr=new ArrayList<>();
		for(int i=0; i<n; i++) {
			arr.add(new ArrayList<>());
		}
		
		for(int i=0; i<m; i++) { // ์–‘๋ฐฉํ–ฅ
			st=new StringTokenizer(bf.readLine());
			int a=Integer.parseInt(st.nextToken())-1;
			int b=Integer.parseInt(st.nextToken())-1;
			arr.get(a).add(b);
			arr.get(b).add(a);
		}
		
		int q=Integer.parseInt(bf.readLine());
		for(int k=0; k<q; k++) {
			st=new StringTokenizer(bf.readLine());
			int a=Integer.parseInt(st.nextToken());
			int i=Integer.parseInt(st.nextToken())-1;
			int j=Integer.parseInt(st.nextToken())-1;
			
			if(a==1) { // ๋„๋กœ ์ƒ์„ฑ
				arr.get(i).add(j);
				arr.get(j).add(i);
			}else { // ๋„๋กœ ์ œ๊ฑฐ
				arr.get(i).remove(Integer.valueOf(j));
				arr.get(j).remove(Integer.valueOf(i));
			}
			
			visited=new boolean[n];
			result=new int[n];
			bfs(0,0);
			for(int l=0; l<n; l++) {
				if(!visited[l]) bw.write("-1 ");
				else bw.write(result[l]+" ");
			}
			bw.write("\n");
		}
		bw.flush();
	}

	private static void bfs(int start, int depth) {
		Queue<int[]> queue=new LinkedList<>();
		queue.add(new int[] {start, depth});
		visited[start]=true;
		
		while(!queue.isEmpty()) {
			int num[]=queue.poll();
			for(int i=0; i<arr.get(num[0]).size(); i++) {
				int temp=arr.get(num[0]).get(i);
				if(!visited[temp]) {
					visited[temp]=true;
					result[temp]=num[1]+1;
					queue.add(new int[] {temp, num[1]+1});
				}
			}
		}
	}
}

 

Main

-  n(๋„์‹œ์˜ ๊ฐœ์ˆ˜), m(๋„๋กœ์˜ ๊ฐœ์ˆ˜) ์ž…๋ ฅ

- arr : ๋„์‹œ ์ •๋ณด ์ €์žฅ

- q(๋„๋กœ ์ •๋น„ ๊ณ„ํš์— ๋“ค์–ด์žˆ๋Š” ๋„๋กœ์˜ ์ˆ˜ ) ์ž…๋ ฅ ๋ฐ a, i, j ์ž…๋ ฅ

- visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

- result: ์ตœ์†Œ ๋ฐฉ๋ฌธ ๋„์‹œ ์ €์žฅ

-  bfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ˆ˜๋„์—์„œ ๋‹ค๋ฅธ ๋„์‹œ ๋ฐฉ๋ฌธ ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ

 

bfs

- queue์— {๋„์‹œ ๋ฒˆํ˜ธ, ๋ฐฉ๋ฌธ ์ˆ˜} ์ €์žฅ

- queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ๋„์‹œ๋ฅผ ํ™•์ธ -> ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ์ด๋™ํ•˜์—ฌ ๋ฐฉ๋ฌธ ํ›„ result์—  ( ๋ฐฉ๋ฌธ ์ˆ˜ + 1 ) ๊ฐ’์„ ์ €์žฅ ๋ฐ queue์— ์ €์žฅํ•ด ์ค€๋‹ค.

 


์ƒ๊ฐ๐Ÿค”

 

๋ฌธ์ œ ํ’€์ด์—์„œ ๋งํ•œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋ดค๋‹ค. ์‹œ๊ฐ„ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.