FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/18/2.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
831
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
18
/
2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (37 loc) · 1.41 KB
Breadcrumbs
python-for-coding-test
/
18
/
2.java
Copy path
File metadata and controls
46 lines (37 loc) · 1.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import
java
.
util
.*;
public
class
Main
{
// 탑승구의 개수와 비행기의 개수
public
static
int
g
,
p
;
public
static
int
[]
parent
=
new
int
[
100001
];
// 부모 테이블 초기화
// 특정 원소가 속한 집합을 찾기
public
static
int
findParent
(
int
x
) {
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
if
(
x
==
parent
[
x
])
return
x
;
return
parent
[
x
] =
findParent
(
parent
[
x
]);
}
// 두 원소가 속한 집합을 합치기
public
static
void
unionParent
(
int
a
,
int
b
) {
a
=
findParent
(
a
);
b
=
findParent
(
b
);
if
(
a
<
b
)
parent
[
b
] =
a
;
else
parent
[
a
] =
b
;
}
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
g
=
sc
.
nextInt
();
p
=
sc
.
nextInt
();
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
for
(
int
i
=
1
;
i
<=
g
;
i
++) {
parent
[
i
] =
i
;
}
int
result
=
0
;
for
(
int
i
=
0
;
i
<
p
;
i
++) {
int
x
=
sc
.
nextInt
();
int
root
=
findParent
(
x
);
// 현재 비행기의 탑승구의 루트 확인
if
(
root
==
0
)
break
;
// 현재 루트가 0이라면, 종료
unionParent
(
root
,
root
-
1
);
// 그렇지 않다면 바로 왼쪽의 집합과 합치기
result
+=
1
;
}
System
.
out
.
println
(
result
);
}
}
Back
|
FazBrowse Home
|
New Git URL