Skip to content

Commit 923606c

Browse files
committed
[Gold IV] Title: 로봇 조립, Time: 1288 ms, Memory: 353664 KB -BaekjoonHub
1 parent b9cba70 commit 923606c

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [Gold IV] 로봇 조립 - 18116
2+
3+
[문제 링크](https://www.acmicpc.net/problem/18116)
4+
5+
### 성능 요약
6+
7+
메모리: 353664 KB, 시간: 1288 ms
8+
9+
### 분류
10+
11+
자료 구조, 분리 집합
12+
13+
### 제출 일자
14+
15+
2024년 11월 13일 16:31:50
16+
17+
### 문제 설명
18+
19+
<p>성규는 로봇을 조립해야 한다. 상자 안에는 여러 로봇의 부품들이 섞여 있다. 그런데 어떤 부품이 어느 로봇의 부품인지 표시가 되어있지 않다. 호재는 전자과라서 두 부품을 보면 같은 로봇의 부품인지 알 수 있다. 그래서 성규는 호재의 지시에 따라 부품들을 정리하기로 하였다.</p>
20+
21+
<p>부품들은 1부터 10<sup>6</sup>까지의 정수로 표현된다. 그리고 부품 <em>i</em>가 속한 로봇은 robot(<em>i</em>)라고도 표현한다. 예를 들어, 부품 11과 부품 22가 로봇 A의 부품이라고 알고 있는 경우, robot(11)은 로봇 A를 의미하고, robot(22)도 로봇 A를 의미한다.</p>
22+
23+
<p>서로 다른 로봇은 공통 부품을 가지지 않는다. 즉 어떤 부품이 로봇 A의 부품이라면, 로봇 B의 부품은 될 수 없다.</p>
24+
25+
<p>호재는 2가지 지시를 한다.</p>
26+
27+
<ul>
28+
<li>서로 다른 부품 2개를 말해주며, 두 부품은 같은 로봇의 부품이라는 정보를 알려준다.</li>
29+
<li>부품 <em>i</em>에 대해서, 지금까지 알아낸 robot(<em>i</em>)의 부품이 몇 개냐고 물어본다.</li>
30+
</ul>
31+
32+
<p>초기에는 부품에 대한 정보가 존재하지 않는다.</p>
33+
34+
### 입력
35+
36+
<p>첫 번째 줄에 호재의 지시 횟수 <em>N</em>이 들어온다. (1 ≤ <em>N</em> ≤ 10<sup>6</sup>)</p>
37+
38+
<p>다음 줄부터 <em>N</em>개의 지시가 들어온다.</p>
39+
40+
<p>부품 2개가 같은 로봇의 부품인지 알려줄 때에는 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D43C TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>I</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">\(I\)</span> </mjx-container><em> a b</em> 의 형태로 들어온다. 부품 <em>a</em>와 부품 <em>b</em>는 같은 로봇의 부품이라는 의미이다. (1 ≤<strong> </strong><em>a</em>, <em>b</em> ≤ 10<sup>6</sup>, <em>a</em> ≠ <em>b, a</em>, <em>b</em>는 정수)</p>
41+
42+
<p>어떤 로봇의 부품이 몇 개인지 물어볼 때에는 <em>Q c</em> 의 형태로 들어온다. 지금까지 알아낸 robot(<em>c</em>)의 부품이 몇 개냐는 의미이다. (1 ≤ <em>c</em> ≤ 10<sup>6</sup>, <em>c</em>는 정수)</p>
43+
44+
<p>입력으로 <em>Q c</em>의 형태가 적어도 한 번 들어온다.</p>
45+
46+
### 출력
47+
48+
<p><em>Q</em>로 시작하는 입력에 대해서 한 줄에 하나씩, 지금까지 알아낸 해당 로봇의 부품 개수를 출력한다.</p>
49+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class Main {
4+
static final int max_idx = 1000001;
5+
static int n;
6+
static ArrayList<Integer>[] graph;
7+
static ArrayDeque<node> deque;
8+
static int[] parent, sub;
9+
static StringTokenizer st;
10+
static StringBuilder sb = new StringBuilder();
11+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
public static void main(String[] args) throws Exception{
14+
pre_setting();
15+
bw.close();
16+
}
17+
18+
static void pre_setting() throws Exception{
19+
n = Integer.parseInt(br.readLine());
20+
deque = new ArrayDeque<>();
21+
22+
graph = new ArrayList[max_idx];
23+
parent = new int[max_idx];
24+
sub = new int[max_idx];
25+
26+
for(int i = 0; i < max_idx; i ++) graph[i] = new ArrayList<>();
27+
Arrays.fill(parent, -1);
28+
Arrays.fill(sub, 1);
29+
30+
int a, b, c;
31+
String q;
32+
for(int i = 0; i < n; i++){
33+
st = new StringTokenizer(br.readLine());
34+
35+
q = st.nextToken();
36+
if(q.equals("I")){
37+
a = Integer.parseInt(st.nextToken());
38+
b = Integer.parseInt(st.nextToken());
39+
40+
deque.add(new node(a, b));
41+
42+
}else{
43+
c = Integer.parseInt(st.nextToken());
44+
sb.append(union_robot(c)).append("\n");
45+
}
46+
}
47+
48+
bw.append(sb);
49+
bw.close();
50+
}
51+
52+
static int union_robot(int ans){
53+
node now;
54+
55+
while(!deque.isEmpty()){
56+
now = deque.poll();
57+
58+
union(now.a, now.b);
59+
}
60+
return sub[find(ans)];
61+
}
62+
63+
static int find(int a){
64+
if(parent[a] == -1) return parent[a] = a;
65+
if(parent[a] == a) return a;
66+
return parent[a] = find(parent[a]);
67+
}
68+
69+
static void union(int a, int b){
70+
a = find(a);
71+
b = find(b);
72+
73+
if(a < b) {
74+
parent[a] = b;
75+
sub[b] += sub[a];
76+
}
77+
else if(b < a){
78+
parent[b] = a;
79+
sub[a] += sub[b];
80+
}
81+
}
82+
83+
static class node{
84+
int a, b;
85+
86+
node(int a, int b){
87+
this.a = a;
88+
this.b = b;
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)