-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐merge: [Week22~23] MST 3๋ฌธ์ + Simulation 2๋ฌธ์ (#85)
* โจfeat: MST ์๊ณ ๋ฆฌ์ฆ ํ์ด * โจfeat: ์๋ฎฌ๋ ์ด์ ์๊ณ ๋ฆฌ์ฆ ํ์ด
- Loading branch information
Showing
5 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package week_22; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.StringTokenizer; | ||
|
||
public class BOJ_1197_์ต์_์คํจ๋_ํธ๋ฆฌ { | ||
|
||
private static int[] parents; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
||
// ์ ๋ ฅ | ||
int vertexCnt = Integer.parseInt(st.nextToken()); | ||
int edgeCnt = Integer.parseInt(st.nextToken()); | ||
|
||
Vertex[] trees = new Vertex[edgeCnt]; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
st = new StringTokenizer(br.readLine(), " "); | ||
Vertex vertex = new Vertex(); | ||
vertex.start = Integer.parseInt(st.nextToken()); | ||
vertex.end = Integer.parseInt(st.nextToken()); | ||
vertex.cost = Integer.parseInt(st.nextToken()); | ||
trees[i] = vertex; | ||
} | ||
|
||
// ๊ฐ์ค์น ๊ธฐ์ค ์ ๋ ฌ | ||
Arrays.sort(trees, (Comparator.comparingLong(o -> o.cost))); | ||
|
||
// ๋ถ๋ชจ ๋ ธ๋ ์ด๊ธฐํ | ||
parents = new int[vertexCnt + 1]; | ||
for (int i = 0; i <= vertexCnt; i++) { | ||
parents[i] = i; | ||
} | ||
|
||
// ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ | ||
int sum = 0; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
Vertex vertex = trees[i]; | ||
int start = vertex.start; | ||
int end = vertex.end; | ||
|
||
if (!sameParent(start, end)) { // ์ฌ์ดํด์ด ํ์ฑ๋์ง ์์ผ๋ฉด | ||
union(start, end); // ๋ถ๋ชจ ๋ ธ๋๋ฅผ ๋ฐ๊ฟ์ฃผ๊ณ | ||
sum += vertex.cost; // ๊ฐ์ค์น ๋ํด์ฃผ๊ธฐ | ||
} | ||
} | ||
|
||
System.out.println(sum); | ||
} | ||
|
||
static class Vertex { | ||
int start; | ||
int end; | ||
long cost; | ||
} | ||
|
||
private static void union(int a, int b) { | ||
if (find(a) > find(b)) { | ||
parents[find(a)] = find(b); | ||
} else { | ||
parents[find(b)] = find(a); | ||
} | ||
} | ||
|
||
private static int find(int a) { | ||
if (parents[a] == a) { | ||
return a; | ||
} | ||
return parents[a] = find(parents[a]); // ??? | ||
} | ||
|
||
private static boolean sameParent(int x, int y) { | ||
return find(x) == find(y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package week_22; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.StringTokenizer; | ||
|
||
public class BOJ_1922_๋คํธ์ํฌ_์ฐ๊ฒฐ { | ||
|
||
private static int[] parents; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// ์ ๋ ฅ | ||
int vertexCnt = Integer.parseInt(br.readLine()); | ||
int edgeCnt = Integer.parseInt(br.readLine()); | ||
|
||
Vertex[] trees = new Vertex[edgeCnt]; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
Vertex vertex = new Vertex(); | ||
|
||
vertex.start = Integer.parseInt(st.nextToken()); | ||
vertex.end = Integer.parseInt(st.nextToken()); | ||
vertex.cost = Integer.parseInt(st.nextToken()); | ||
|
||
trees[i] = vertex; | ||
} | ||
// ์ ๋ ฌ | ||
Arrays.sort(trees, Comparator.comparingInt(o -> o.cost)); | ||
|
||
// ๋ถ๋ชจ ๋ ธ๋ ์ด๊ธฐํ | ||
parents = new int[vertexCnt + 1]; | ||
for (int i = 0; i <= vertexCnt; i++) { | ||
parents[i] = i; | ||
} | ||
|
||
// ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ | ||
int cost = 0; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
Vertex vertex = trees[i]; | ||
int start = vertex.start; | ||
int end = vertex.end; | ||
|
||
if (!sameParent(start, end)) { | ||
union(start, end); | ||
cost += vertex.cost; | ||
} | ||
} | ||
System.out.println(cost); | ||
} | ||
|
||
static class Vertex { | ||
int start; | ||
int end; | ||
int cost; | ||
} | ||
|
||
private static boolean sameParent(int x, int y) { | ||
return find(x) == find(y); | ||
} | ||
|
||
private static int find(int vertex) { | ||
if (parents[vertex] == vertex) { // ์ฌ๊ธฐ๋ ์ ์๊ธฐ ์์ ์ผ ๋๋ ๊ทธ๋ฅ ๋์๊ฐ๋์ง ๋ชจ๋ฅด๊ฒ ์ | ||
return vertex; | ||
} | ||
return parents[vertex] = find(parents[vertex]); // ์ฌ๊ธฐ ์ดํด ๋ชปํจ | ||
} | ||
|
||
private static void union(int x, int y) { | ||
int xParent = find(x); | ||
int yParent = find(y); | ||
|
||
if (xParent > yParent) { | ||
parents[x] = yParent; // ์ฌ๊ธฐ ์๋ชป๋จ | ||
} else { | ||
parents[y] = xParent; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package week_22; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.StringTokenizer; | ||
|
||
// TODO: ์ดํด๊ฐ ๊ฐ์ง ์๋ ์ : union find ํ๋ฉด์ ๋ถ๋ชจ ๋ ธ๋๊ฐ ๋ค ๋๊ฐ์ด ๋ณํด์ผ ํ๋ ๊ฒ ์๋๊ฐ? ์ 2๋ฒ ์์ ๋ ๋ถ๋ชจ ๋ ธ๋๊ฐ ๋ค๋ฅด์ง???? | ||
// TODO: ์ด์ํ ์ ๋ ฅ ์กฐ๊ฑด ๋จธ๋ ์ ๊ฑฐ,, ใ | ||
|
||
public class BOJ_21924_๋์_๊ฑด์ค { | ||
|
||
private static int[] parents; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
||
// ์ ๋ ฅ | ||
int vertexCnt = Integer.parseInt(st.nextToken()); | ||
int edgeCnt = Integer.parseInt(st.nextToken()); | ||
|
||
long totalCost = 0; | ||
Vertex[] trees = new Vertex[edgeCnt]; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
st = new StringTokenizer(br.readLine(), " "); | ||
Vertex vertex = new Vertex(); | ||
|
||
vertex.start = Integer.parseInt(st.nextToken()); | ||
vertex.end = Integer.parseInt(st.nextToken()); | ||
vertex.cost = Integer.parseInt(st.nextToken()); | ||
|
||
trees[i] = vertex; | ||
totalCost += vertex.cost; | ||
} | ||
|
||
// ๊ฐ์ค์น ๊ธฐ์ค ์ ๋ ฌ | ||
Arrays.sort(trees, (Comparator.comparingInt(o -> o.cost))); | ||
|
||
// ๋ถ๋ชจ ๋ ธ๋ ์ด๊ธฐํ | ||
parents = new int[vertexCnt + 1]; | ||
for (int i = 0; i <= vertexCnt; i++) { | ||
parents[i] = i; | ||
} | ||
|
||
// ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ | ||
long minCost = 0; | ||
int unionCnt = 0; | ||
for (int i = 0; i < edgeCnt; i++) { | ||
Vertex vertex = trees[i]; | ||
int start = vertex.start; | ||
int end = vertex.end; | ||
|
||
if (!sameParent(start, end)) { | ||
union(start, end); | ||
minCost += vertex.cost; | ||
unionCnt++; | ||
} | ||
} | ||
if (unionCnt != vertexCnt - 1) { | ||
System.out.println(-1); | ||
return; | ||
} | ||
System.out.println(totalCost - minCost); | ||
} | ||
|
||
static class Vertex { | ||
int start; | ||
int end; | ||
int cost; | ||
} | ||
|
||
private static void union(int a, int b) { | ||
if (find(a) > find(b)) { | ||
parents[find(a)] = find(b); | ||
} else { | ||
parents[find(b)] = find(a); | ||
} | ||
} | ||
|
||
private static int find(int a) { | ||
if (parents[a] == a) { | ||
return a; | ||
} | ||
return parents[a] = find(parents[a]); // ์ด๋ฐ ๋ฌธ๋ฒ ์ฒ์๋ด,, | ||
} | ||
|
||
private static boolean sameParent(int x, int y) { | ||
return find(x) == find(y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package week_23; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.StringTokenizer; | ||
|
||
public class BOJ_1713_ํ๋ณด_์ถ์ฒํ๊ธฐ { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// ์ ๋ ฅ | ||
int postSize = Integer.parseInt(br.readLine()); | ||
int votes = Integer.parseInt(br.readLine()); | ||
StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
||
// ์ต์ข ํ๋ณด ํ์ธ | ||
Student[] students = new Student[101]; // ๋ชจ๋ ํ์์ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฐฐ์ด | ||
List<Student> post = new ArrayList<>(); // ์ฌ์งํ์ ๊ฒ์๋ ์ต์ข ํ๋ณด์ ํ์ ์ ๋ณด๋ฅผ ๋ด๋ ๋ฐฐ์ด | ||
|
||
for (int i = 0; i < votes; i++) { | ||
int vote = Integer.parseInt(st.nextToken()); | ||
|
||
if (students[vote] == null) { // ์ฒ์ ์ถ์ฒ๋ฐ์๋ค๋ฉด ๊ฐ์ฒด ์์ฑ | ||
students[vote] = new Student(vote, 0, 0, false); | ||
} | ||
|
||
if (students[vote].isPosted) { // ์ด๋ฏธ ๊ฒ์๋์ด ์์ผ๋ฉด ์ถ์ฒ์๋ง 1์ฆ๊ฐ | ||
students[vote].count++; // 4๋ฒ ์กฐ๊ฑด ์ฃผ์ | ||
continue; | ||
} | ||
|
||
if (post.size() < postSize) { // ์ฌ์งํ์ด ๋น์ด์์ผ๋ฉด | ||
post.add(students[vote]); // ๊ฒ์ํ๊ณ | ||
|
||
} else { // ์ฌ์ง๋ค์ด ์ฐจ์์ผ๋ฉด | ||
Collections.sort(post); // ์ถ์ฒ์, ์๊ฐ์์ผ๋ก ์ ๋ ฌํ๊ณ | ||
post.get(0).isPosted = false; // ๊ฒ์ ์ฌ๋ถ ์ ๋ณด ๋ฐ๊พธ๊ณ | ||
post.get(0).count = 0; // ์ถ์ฒ ์ 0์ผ๋ก ๋ณ๊ฒฝ | ||
post.remove(0); // ๊ธฐ์กด ํ์ ์ ๊ฑฐํ๊ณ | ||
post.add(students[vote]); // ์๋ก์ด ํ์ ๊ฒ์ | ||
} | ||
|
||
// ์ถ์ฒ๋ ํ์ ์ ๋ณด ์ ๋ฐ์ดํธ | ||
students[vote].count++; // ์ถ์ฒ์ 1 ์ฆ๊ฐํ๊ณ | ||
students[vote].time = i; // ํฌ์คํ ์๊ฐ ์ ๋ฐ์ดํธํ๊ณ | ||
students[vote].isPosted = true; // ํฌ์คํ ์ฌ๋ถ ์ ๋ฐ์ดํธ | ||
} | ||
|
||
// ์ถ๋ ฅ | ||
post.sort(Comparator.comparingInt(o -> o.index)); | ||
post.forEach(student -> System.out.print(student.index + " ")); | ||
} | ||
|
||
static class Student implements Comparable<Student> { | ||
int index; | ||
int count; | ||
int time; | ||
boolean isPosted; | ||
|
||
public Student(int index, int count, int time, boolean isPosted) { | ||
this.index = index; | ||
this.count = count; | ||
this.time = time; | ||
this.isPosted = isPosted; | ||
} | ||
|
||
@Override | ||
public int compareTo(Student o) { | ||
if (this.count == o.count) { | ||
return this.time - o.time; | ||
} | ||
return this.count - o.count; | ||
} | ||
} | ||
} |
Oops, something went wrong.