Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- YBMCOS
- 취득후기
- GatherTown
- 백준코딩테스트
- 01BFS
- 다익스트라
- 구현
- 네트워크플로우
- 이젠 골드구현도 어렵네..
- 자바PS
- QUICKSTARTGUIDE
- PS
- 시뮬레이션
- 백준
- java
- 게더타운시작
- 우선순위큐
- deque
- COSPROJAVA1급
- dp
- 엘라스틱서치
- 세그먼트트리
- 다이나믹프로그래밍
- BFS
- 재귀함수
- 완전탐색
- spring
- COSPRO
- DFS
- 알고리즘
Archives
- Today
- Total
공부공간
BOJ - 13463 ) Brexit 본문
https://www.acmicpc.net/problem/13463
여러개의 행성이 존재하는데, 한 행성이 union을 탈출하면서 해당 행성과 연결되어있는
행성들의 간선개수를 한개씩 줄여준다. 만약 줄여주는 과정에서, 최초로 연결된 간선의 개수의 절반보다
적어진다면, 해당 행성도 탈출한다. 이때 x 인 내가 탈출대상인지를 물어보는 문제이다.
위상정렬으로도 풀수있을것같은데.. 일단은 BFS를 돌면서 간선개수를 줄여주는 식으로 진행하였다.
package algorithm_2022;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class BOJ_13463 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int l = Integer.parseInt(st.nextToken());
ArrayList<Integer> al[] = new ArrayList[c+1];
for(int i=1;i<=c;i++) {al[i] = new ArrayList<>();}
for(int i=0;i<p;i++) {
st= new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
al[e].add(s); al[s].add(e);
}
int line[] = new int[c+1];
int count[] = new int[c+1];
boolean leave[] = new boolean[c+1];
for(int i=1;i<=c;i++) {
line[i] = al[i].size();
} // count of connected lines
ArrayDeque<Integer> dq = new ArrayDeque<>();
dq.add(l);
leave[l] = true;
while(!dq.isEmpty()) {
int now = dq.poll();
int size = al[now].size();
for(int i=0;i<size;i++) {
int next = al[now].get(i);
count[next]++;
if(2*count[next] >=line[next] && !leave[next]) {
dq.add(next);
leave[next] = true;
}
}
}
System.out.println(leave[x] ? "leave":"stay");
}
}
'알고리즘 > 완전탐색(BFS,DFS)' 카테고리의 다른 글
BOJ_16928 ) 뱀과 사다리 게임 (1) | 2022.03.11 |
---|---|
BOJ - 23747 ) 와드 (0) | 2022.01.25 |
BOJ - 1584 ) 게임 (0) | 2021.10.17 |
BOJ - 5014 ) 스타트링크 (0) | 2021.08.21 |
BOJ - 15812 ) 침략자 진아 (0) | 2021.05.13 |
Comments