Skip to content

Commit d429ee2

Browse files
committed
Update
1 parent b6bedd4 commit d429ee2

File tree

8 files changed

+573
-0
lines changed

8 files changed

+573
-0
lines changed

10/1.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 노드의 개수(V)와 간선(Union 연산)의 개수(E)
6+
// 노드의 개수는 최대 100,000개라고 가정
7+
public static int v, e;
8+
public static int[] parent = new int[100001]; // 부모 테이블 초기화하기
9+
10+
// 특정 원소가 속한 집합을 찾기
11+
public static int findParent(int x) {
12+
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
13+
if (x == parent[x]) return x;
14+
return findParent(parent[x]);
15+
}
16+
17+
// 두 원소가 속한 집합을 합치기
18+
public static void unionParent(int a, int b) {
19+
a = findParent(a);
20+
b = findParent(b);
21+
if (a < b) parent[b] = a;
22+
else parent[a] = b;
23+
}
24+
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner(System.in);
27+
28+
v = sc.nextInt();
29+
e = sc.nextInt();
30+
31+
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
32+
for (int i = 1; i <= v; i++) {
33+
parent[i] = i;
34+
}
35+
36+
// Union 연산을 각각 수행
37+
for (int i = 0; i < e; i++) {
38+
int a = sc.nextInt();
39+
int b = sc.nextInt();
40+
unionParent(a, b);
41+
}
42+
43+
// 각 원소가 속한 집합 출력하기
44+
System.out.print("각 원소가 속한 집합: ");
45+
for (int i = 1; i <= v; i++) {
46+
System.out.print(findParent(i) + " ");
47+
}
48+
System.out.println();
49+
50+
// 부모 테이블 내용 출력하기
51+
System.out.print("부모 테이블: ");
52+
for (int i = 1; i <= v; i++) {
53+
System.out.print(parent[i] + " ");
54+
}
55+
System.out.println();
56+
}
57+
}

10/3.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 노드의 개수(V)와 간선(Union 연산)의 개수(E)
6+
// 노드의 개수는 최대 100,000개라고 가정
7+
public static int v, e;
8+
public static int[] parent = new int[100001]; // 부모 테이블 초기화하기
9+
10+
// 특정 원소가 속한 집합을 찾기
11+
public static int findParent(int x) {
12+
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
13+
if (x == parent[x]) return x;
14+
return parent[x] = findParent(parent[x]);
15+
}
16+
17+
// 두 원소가 속한 집합을 합치기
18+
public static void unionParent(int a, int b) {
19+
a = findParent(a);
20+
b = findParent(b);
21+
if (a < b) parent[b] = a;
22+
else parent[a] = b;
23+
}
24+
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner(System.in);
27+
28+
v = sc.nextInt();
29+
e = sc.nextInt();
30+
31+
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
32+
for (int i = 1; i <= v; i++) {
33+
parent[i] = i;
34+
}
35+
36+
// Union 연산을 각각 수행
37+
for (int i = 0; i < e; i++) {
38+
int a = sc.nextInt();
39+
int b = sc.nextInt();
40+
unionParent(a, b);
41+
}
42+
43+
// 각 원소가 속한 집합 출력하기
44+
System.out.print("각 원소가 속한 집합: ");
45+
for (int i = 1; i <= v; i++) {
46+
System.out.print(findParent(i) + " ");
47+
}
48+
System.out.println();
49+
50+
// 부모 테이블 내용 출력하기
51+
System.out.print("부모 테이블: ");
52+
for (int i = 1; i <= v; i++) {
53+
System.out.print(parent[i] + " ");
54+
}
55+
System.out.println();
56+
}
57+
}

10/4.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 노드의 개수(V)와 간선(Union 연산)의 개수(E)
6+
// 노드의 개수는 최대 100,000개라고 가정
7+
public static int v, e;
8+
public static int[] parent = new int[100001]; // 부모 테이블 초기화하기
9+
10+
// 특정 원소가 속한 집합을 찾기
11+
public static int findParent(int x) {
12+
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
13+
if (x == parent[x]) return x;
14+
return parent[x] = findParent(parent[x]);
15+
}
16+
17+
// 두 원소가 속한 집합을 합치기
18+
public static void unionParent(int a, int b) {
19+
a = findParent(a);
20+
b = findParent(b);
21+
if (a < b) parent[b] = a;
22+
else parent[a] = b;
23+
}
24+
25+
public static void main(String[] args) {
26+
Scanner sc = new Scanner(System.in);
27+
28+
v = sc.nextInt();
29+
e = sc.nextInt();
30+
31+
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
32+
for (int i = 1; i <= v; i++) {
33+
parent[i] = i;
34+
}
35+
36+
boolean cycle = false; // 사이클 발생 여부
37+
38+
for (int i = 0; i < e; i++) {
39+
int a = sc.nextInt();
40+
int b = sc.nextInt();
41+
// 사이클이 발생한 경우 종료
42+
if (findParent(a) == findParent(b)) {
43+
cycle = true;
44+
break;
45+
}
46+
// 사이클이 발생하지 않았다면 합집합(Union) 연산 수행
47+
else {
48+
unionParent(a, b);
49+
}
50+
}
51+
52+
if (cycle) {
53+
System.out.println("사이클이 발생했습니다.");
54+
}
55+
else {
56+
System.out.println("사이클이 발생하지 않았습니다.");
57+
}
58+
}
59+
}

10/5.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.util.*;
2+
3+
class Edge implements Comparable<Edge> {
4+
5+
private int distance;
6+
private int nodeA;
7+
private int nodeB;
8+
9+
public Edge(int distance, int nodeA, int nodeB) {
10+
this.distance = distance;
11+
this.nodeA = nodeA;
12+
this.nodeB = nodeB;
13+
}
14+
15+
public int getDistance() {
16+
return this.distance;
17+
}
18+
19+
public int getNodeA() {
20+
return this.nodeA;
21+
}
22+
23+
public int getNodeB() {
24+
return this.nodeB;
25+
}
26+
27+
// 거리(비용)가 짧은 것이 높은 우선순위를 가지도록 설정
28+
@Override
29+
public int compareTo(Edge other) {
30+
if (this.distance < other.distance) {
31+
return -1;
32+
}
33+
return 1;
34+
}
35+
}
36+
37+
public class Main {
38+
39+
// 노드의 개수(V)와 간선(Union 연산)의 개수(E)
40+
// 노드의 개수는 최대 100,000개라고 가정
41+
public static int v, e;
42+
public static int[] parent = new int[100001]; // 부모 테이블 초기화하기
43+
// 모든 간선을 담을 리스트와, 최종 비용을 담을 변수
44+
public static ArrayList<Edge> edges = new ArrayList<>();
45+
public static int result = 0;
46+
47+
// 특정 원소가 속한 집합을 찾기
48+
public static int findParent(int x) {
49+
// 루트 노드가 아니라면, 루트 노드를 찾을 때까지 재귀적으로 호출
50+
if (x == parent[x]) return x;
51+
return parent[x] = findParent(parent[x]);
52+
}
53+
54+
// 두 원소가 속한 집합을 합치기
55+
public static void unionParent(int a, int b) {
56+
a = findParent(a);
57+
b = findParent(b);
58+
if (a < b) parent[b] = a;
59+
else parent[a] = b;
60+
}
61+
62+
public static void main(String[] args) {
63+
Scanner sc = new Scanner(System.in);
64+
65+
v = sc.nextInt();
66+
e = sc.nextInt();
67+
68+
// 부모 테이블상에서, 부모를 자기 자신으로 초기화
69+
for (int i = 1; i <= v; i++) {
70+
parent[i] = i;
71+
}
72+
73+
// 모든 간선에 대한 정보를 입력 받기
74+
for (int i = 0; i < e; i++) {
75+
int a = sc.nextInt();
76+
int b = sc.nextInt();
77+
int cost = sc.nextInt();
78+
// 비용순으로 정렬하기 위해서 튜플의 첫 번째 원소를 비용으로 설정
79+
edges.add(new Edge(cost, a, b));
80+
}
81+
82+
// 간선을 비용순으로 정렬
83+
Collections.sort(edges);
84+
85+
// 간선을 하나씩 확인하며
86+
for (int i = 0; i < edges.size(); i++) {
87+
int cost = edges.get(i).getDistance();
88+
int a = edges.get(i).getNodeA();
89+
int b = edges.get(i).getNodeB();
90+
// 사이클이 발생하지 않는 경우에만 집합에 포함
91+
if (findParent(a) != findParent(b)) {
92+
unionParent(a, b);
93+
result += cost;
94+
}
95+
}
96+
97+
System.out.println(result);
98+
}
99+
}

10/6.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 노드의 개수(V)와 간선의 개수(E)
6+
// 노드의 개수는 최대 100,000개라고 가정
7+
public static int v, e;
8+
// 모든 노드에 대한 진입차수는 0으로 초기화
9+
public static int[] indegree = new int[100001];
10+
// 각 노드에 연결된 간선 정보를 담기 위한 연결 리스트 초기화
11+
public static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
12+
13+
// 위상 정렬 함수
14+
public static void topologySort() {
15+
ArrayList<Integer> result = new ArrayList<>(); // 알고리즘 수행 결과를 담을 리스트
16+
Queue<Integer> q = new LinkedList<>(); // 큐 라이브러리 사용
17+
18+
// 처음 시작할 때는 진입차수가 0인 노드를 큐에 삽입
19+
for (int i = 1; i <= v; i++) {
20+
if (indegree[i] == 0) {
21+
q.offer(i);
22+
}
23+
}
24+
25+
// 큐가 빌 때까지 반복
26+
while (!q.isEmpty()) {
27+
// 큐에서 원소 꺼내기
28+
int now = q.poll();
29+
result.add(now);
30+
// 해당 원소와 연결된 노드들의 진입차수에서 1 빼기
31+
for (int i = 0; i < graph.get(now).size(); i++) {
32+
indegree[graph.get(now).get(i)] -= 1;
33+
// 새롭게 진입차수가 0이 되는 노드를 큐에 삽입
34+
if (indegree[graph.get(now).get(i)] == 0) {
35+
q.offer(graph.get(now).get(i));
36+
}
37+
}
38+
}
39+
40+
// 위상 정렬을 수행한 결과 출력
41+
for (int i = 0; i < result.size(); i++) {
42+
System.out.print(result.get(i) + " ");
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
Scanner sc = new Scanner(System.in);
48+
49+
v = sc.nextInt();
50+
e = sc.nextInt();
51+
52+
// 그래프 초기화
53+
for (int i = 0; i <= v; i++) {
54+
graph.add(new ArrayList<Integer>());
55+
}
56+
57+
// 방향 그래프의 모든 간선 정보를 입력 받기
58+
for (int i = 0; i < e; i++) {
59+
int a = sc.nextInt();
60+
int b = sc.nextInt();
61+
graph.get(a).add(b); // 정점 A에서 B로 이동 가능
62+
// 진입 차수를 1 증가
63+
indegree[b] += 1;
64+
}
65+
66+
topologySort();
67+
}
68+
}

0 commit comments

Comments
 (0)