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 | 29 | 30 |
Tags
- batch job
- DP - 유한배낭
- cfieldname
- DP - 무한배낭(순서)
- BOJ_Gold
- using value
- APPENDING CORRESPONDING
- Data Browser
- Union-Find
- SM36
- CTS #CTS 이관 #SAP #ABAP
- 날짜 계산 함수
- ZPL
- qfieldname
- DP - 무한배낭
- SAP GUI
- MONAT_F4
- java
- transporting
- changing value
- FOR ALL ENTRIES IN
- SAP
- 필드카탈로그
- Dictionary Search Help
- READ TABLE
- NEW-PAGE PRINT ON
- APPENDING
- boole_d
- ALV Output Setting
- ABAP
Archives
- Today
- Total
Jin's Library
[Gold Ⅴ] 1717 - 집합의 표현 본문
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_1717 {
static int[] parent;
static int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]); // 경로압축
}
static void union(int a, int b){
a = find(a);
b = find(b);
if (a != b) parent[b] = a;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
parent = new int[N+1];
for(int i = 1; i <= N; i++) parent[i] = i;
for(int i = 0; i < M; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
if(x == 0)
union(a, b);
else{
if(find(a) == find(b)) sb.append("YES").append("\n");
else sb.append("NO").append("\n");
}
}
System.out.println(sb);
}
}'Algorithm - Java > BOJ - Gold' 카테고리의 다른 글
| [Gold Ⅳ] 1043 - 거짓말 (0) | 2025.10.02 |
|---|---|
| [Gold Ⅱ] 10775 - 공항 (0) | 2025.10.01 |
| [Gold Ⅳ] 1976 - 여행 가자 (0) | 2025.10.01 |
| [Gold Ⅳ] 17298 - 오큰수 (0) | 2025.09.30 |
| [Gold Ⅳ] 16120 - PPAP (0) | 2025.09.29 |