Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-for-coding-test/18/4.java at master · SeongbinPark/python-for-coding-test · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
SeongbinPark
/
python-for-coding-test
Public
forked from
ndb796/python-for-coding-test
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
python-for-coding-test
/
18
/
4.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
141 lines (114 loc) · 3.89 KB
master
Breadcrumbs
python-for-coding-test
/
18
/
4.java
Copy path
Top
File metadata and controls
Code
Blame
141 lines (114 loc) · 3.89 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import
java
.
util
.*;
class
Edge
implements
Comparable
<
Edge
> {
private
int
distance
;
private
int
nodeA
;
private
int
nodeB
;
public
Edge
(
int
distance
,
int
nodeA
,
int
nodeB
) {
this
.
distance
=
distance
;
this
.
nodeA
=
nodeA
;
this
.
nodeB
=
nodeB
;
}
public
int
getDistance
() {
return
this
.
distance
;
}
public
int
getNodeA
() {
return
this
.
nodeA
;
}
public
int
getNodeB
() {
return
this
.
nodeB
;
}
// 거리(비용)가 짧은 것이 높은 우선순위를 가지도록 설정
@
Override
public
int
compareTo
(
Edge
other
) {
if
(
this
.
distance
<
other
.
distance
) {
return
-
1
;
}
return
1
;
}
}
class
Position
implements
Comparable
<
Position
> {
private
int
x
;
private
int
y
;
public
Position
(
int
x
,
int
y
) {
this
.
x
=
x
;
this
.
y
=
y
;
}
public
int
getX
() {
return
this
.
x
;
}
public
int
getY
() {
return
this
.
y
;
}
// X축, Y축 순서대로 정렬
@
Override
public
int
compareTo
(
Position
other
) {
if
(
this
.
x
==
other
.
x
) {
return
Integer
.
compare
(
this
.
y
,
other
.
y
);
}
return
Integer
.
compare
(
this
.
x
,
other
.
x
);
}
}
public
class
Main
{
// 노드의 개수
public
static
int
n
;
public
static
int
[]
parent
=
new
int
[
100001
];
// 부모 테이블 초기화
// 모든 간선을 담을 리스트와, 최종 비용을 담을 변수
public
static
ArrayList
<
Edge
>
edges
=
new
ArrayList
<>();
public
static
int
result
=
0
;
// 특정 원소가 속한 집합을 찾기
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
);
n
=
sc
.
nextInt
();
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
for
(
int
i
=
1
;
i
<=
n
;
i
++) {
parent
[
i
] =
i
;
}
ArrayList
<
Position
>
x
=
new
ArrayList
<
Position
>();
ArrayList
<
Position
>
y
=
new
ArrayList
<
Position
>();
ArrayList
<
Position
>
z
=
new
ArrayList
<
Position
>();
// 모든 노드에 대한 좌표 값 입력받기
for
(
int
i
=
1
;
i
<=
n
;
i
++) {
int
a
=
sc
.
nextInt
();
int
b
=
sc
.
nextInt
();
int
c
=
sc
.
nextInt
();
x
.
add
(
new
Position
(
a
,
i
));
y
.
add
(
new
Position
(
b
,
i
));
z
.
add
(
new
Position
(
c
,
i
));
}
Collections
.
sort
(
x
);
Collections
.
sort
(
y
);
Collections
.
sort
(
z
);
// 인접한 노드들로부터 간선 정보를 추출하여 처리
for
(
int
i
=
0
;
i
<
n
-
1
;
i
++) {
edges
.
add
(
new
Edge
(
x
.
get
(
i
+
1
).
getX
() -
x
.
get
(
i
).
getX
(),
x
.
get
(
i
).
getY
(),
x
.
get
(
i
+
1
).
getY
()));
edges
.
add
(
new
Edge
(
y
.
get
(
i
+
1
).
getX
() -
y
.
get
(
i
).
getX
(),
y
.
get
(
i
).
getY
(),
y
.
get
(
i
+
1
).
getY
()));
edges
.
add
(
new
Edge
(
z
.
get
(
i
+
1
).
getX
() -
z
.
get
(
i
).
getX
(),
z
.
get
(
i
).
getY
(),
z
.
get
(
i
+
1
).
getY
()));
}
// 간선을 비용순으로 정렬
Collections
.
sort
(
edges
);
// 간선을 하나씩 확인하며
for
(
int
i
=
0
;
i
<
edges
.
size
();
i
++) {
int
cost
=
edges
.
get
(
i
).
getDistance
();
int
a
=
edges
.
get
(
i
).
getNodeA
();
int
b
=
edges
.
get
(
i
).
getNodeB
();
// 사이클이 발생하지 않는 경우에만 집합에 포함
if
(
findParent
(
a
) !=
findParent
(
b
)) {
unionParent
(
a
,
b
);
result
+=
cost
;
}
}
System
.
out
.
println
(
result
);
}
}
You can’t perform that action at this time.