Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed Members/JongWon/Week12/.keep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int N, M;
private static boolean[] isUsed = new boolean[10];
private static int arr[] = new int[10];

public static void main(String[] args) throws Exception {
int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
N = input[0];
M = input[1];
StringBuilder sb = new StringBuilder();
tracking(1, 0, sb);
System.out.println(sb);
}

private static void tracking(int at, int num, StringBuilder sb) {
if (num == M) {
for (int i = 0; i < M; i++) {
sb.append(arr[i] + " ");
}
sb.append("\n");
return;
}

for (int i = at; i <= N; i++) {
if (!isUsed[i]) {
arr[num] = i;
tracking(i, num + 1, sb);
}
}
}
}
36 changes: 36 additions & 0 deletions Members/JongWon/Week12/15652. N과 M (4)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Silver III] N과 M (4) - 15652

[문제 링크](https://www.acmicpc.net/problem/15652)

### 성능 요약

메모리: 21232 KB, 시간: 228 ms

### 분류

백트래킹

### 문제 설명

<p>자연수 N과 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>

<ul>
<li>1부터 N까지 자연수 중에서 M개를 고른 수열</li>
<li>같은 수를 여러 번 골라도 된다.</li>
<li>고른 수열은 비내림차순이어야 한다.
<ul>
<li>길이가 K인 수열 A가 A<sub>1</sub> ≤ A<sub>2</sub> ≤ ... ≤ A<sub>K-1</sub> ≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</li>
</ul>
</li>
</ul>

### 입력

<p>첫째 줄에 자연수 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>

### 출력

<p>한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p>

<p>수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>

34 changes: 34 additions & 0 deletions Members/JongWon/Week12/1967. 트리의 지름/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# [Gold IV] 트리의 지름 - 1967

[문제 링크](https://www.acmicpc.net/problem/1967)

### 성능 요약

메모리: 23728 KB, 시간: 300 ms

### 분류

깊이 우선 탐색, 그래프 이론, 그래프 탐색, 트리

### 문제 설명

<p>트리(tree)는 사이클이 없는 무방향 그래프이다. 트리에서는 어떤 두 노드를 선택해도 둘 사이에 경로가 항상 하나만 존재하게 된다. 트리에서 어떤 두 노드를 선택해서 양쪽으로 쫙 당길 때, 가장 길게 늘어나는 경우가 있을 것이다. 이럴 때 트리의 모든 노드들은 이 두 노드를 지름의 끝 점으로 하는 원 안에 들어가게 된다.</p>

<p><img alt="" height="123" src="https://www.acmicpc.net/JudgeOnline/upload/201007/ttrrtrtr.png" width="310"></p>

<p>이런 두 노드 사이의 경로의 길이를 트리의 지름이라고 한다. 정확히 정의하자면 트리에 존재하는 모든 경로들 중에서 가장 긴 것의 길이를 말한다.</p>

<p>입력으로 루트가 있는 트리를 가중치가 있는 간선들로 줄 때, 트리의 지름을 구해서 출력하는 프로그램을 작성하시오. 아래와 같은 트리가 주어진다면 트리의 지름은 45가 된다.</p>

<p><img alt="" height="152" src="https://www.acmicpc.net/JudgeOnline/upload/201007/tttttt.png" width="312"></p>

<p>트리의 노드는 1부터 n까지 번호가 매겨져 있다.</p>

### 입력

<p>파일의 첫 번째 줄은 노드의 개수 n(1 ≤ n ≤ 10,000)이다. 둘째 줄부터 n-1개의 줄에 각 간선에 대한 정보가 들어온다. 간선에 대한 정보는 세 개의 정수로 이루어져 있다. 첫 번째 정수는 간선이 연결하는 두 노드 중 부모 노드의 번호를 나타내고, 두 번째 정수는 자식 노드를, 세 번째 정수는 간선의 가중치를 나타낸다. 간선에 대한 정보는 부모 노드의 번호가 작은 것이 먼저 입력되고, 부모 노드의 번호가 같으면 자식 노드의 번호가 작은 것이 먼저 입력된다. 루트 노드의 번호는 항상 1이라고 가정하며, 간선의 가중치는 100보다 크지 않은 양의 정수이다.</p>

### 출력

<p>첫째 줄에 트리의 지름을 출력한다.</p>

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.stream.IntStream;

public class Main {

private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws Exception {
int N = Integer.parseInt(br.readLine());
List<List<Node>> list = new ArrayList<>();

for (int i = 0; i <= N; i++) {
list.add(new ArrayList<>());
}

StringTokenizer st;
for (int i = 0; i < N - 1; i++) {
st = new StringTokenizer(br.readLine());
int parent = Integer.parseInt(st.nextToken());
int child = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());

list.get(parent).add(new Node(child, weight));
list.get(child).add(new Node(parent, weight));
}
int[] visited = bfs(IntStream.range(0, N + 1).map(i -> -1).toArray(), list, 1);

// 1에서 가장 먼곳 찾기
int max = Integer.MIN_VALUE;
int maxIdx = Integer.MIN_VALUE;
for (int i = 0; i <= N; i++) {
if (max < visited[i]) {
max = visited[i];
maxIdx = i;
}
}
// 가장 먼곳에서 가장 먼곳 찾기
visited = bfs(IntStream.range(0, N + 1).map(i -> -1).toArray(), list, maxIdx);
System.out.println(Arrays.stream(visited).max().orElse(-1));
}

static int[] bfs(int[] visited, List<List<Node>> list, int start) {
Queue<Integer> q = new LinkedList<>();
visited[start] = 0;
q.offer(start);
while (!q.isEmpty()) {
int temp = q.poll();
for (Node near : list.get(temp)) {
if (visited[near.num] != -1)
continue;
visited[near.num] = visited[temp] + near.weight;
q.offer(near.num);
}
}
return visited;
}

static class Node {
int num;
int weight;

Node(int num, int weight) {
this.num = num;
this.weight = weight;
}
}

}
36 changes: 36 additions & 0 deletions Members/JongWon/Week12/5639. 이진 검색 트리/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Gold V] 이진 검색 트리 - 5639

[문제 링크](https://www.acmicpc.net/problem/5639)

### 성능 요약

메모리: 20152 KB, 시간: 492 ms

### 분류

그래프 이론, 그래프 탐색, 재귀, 트리

### 문제 설명

<p>이진 검색 트리는 다음과 같은 세 가지 조건을 만족하는 이진 트리이다.</p>

<ul>
<li>노드의 왼쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 작다.</li>
<li>노드의 오른쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 크다.</li>
<li>왼쪽, 오른쪽 서브트리도 이진 검색 트리이다.</li>
</ul>

<p style="text-align: center;"><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/upload/images/bsearchtree.png" style="height:242px; width:426px"></p>

<p>전위 순회 (루트-왼쪽-오른쪽)은 루트를 방문하고, 왼쪽 서브트리, 오른쪽 서브 트리를 순서대로 방문하면서 노드의 키를 출력한다. 후위 순회 (왼쪽-오른쪽-루트)는 왼쪽 서브트리, 오른쪽 서브트리, 루트 노드 순서대로 키를 출력한다. 예를 들어, 위의 이진 검색 트리의 전위 순회 결과는 50 30 24 5 28 45 98 52 60 이고, 후위 순회 결과는 5 28 24 45 30 60 52 98 50 이다.</p>

<p>이진 검색 트리를 전위 순회한 결과가 주어졌을 때, 이 트리를 후위 순회한 결과를 구하는 프로그램을 작성하시오.</p>

### 입력

<p>트리를 전위 순회한 결과가 주어진다. 노드에 들어있는 키의 값은 10<sup>6</sup>보다 작은 양의 정수이다. 모든 값은 한 줄에 하나씩 주어지며, 노드의 수는 10,000개 이하이다. 같은 키를 가지는 노드는 없다.</p>

### 출력

<p>입력으로 주어진 이진 검색 트리를 후위 순회한 결과를 한 줄에 하나씩 출력한다.</p>

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws Exception {
List<Integer> inputList = new ArrayList<>();
String line;
while (true) {
line = br.readLine();
if (line == null || line.equals(""))
break;
inputList.add(Integer.parseInt(line));
}

Node root = new Node(inputList.get(0));
for (int i = 1; i < inputList.size(); i++) {
root.insert(inputList.get(i));
}
System.out.println(postOrder(new StringBuilder(), root));
}

static StringBuilder postOrder(StringBuilder sb, Node node) {
if (node == null)
return sb;
postOrder(sb, node.left);
postOrder(sb, node.right);
sb.append(node.num + "\n");
return sb;
}

static class Node {
int num;
Node left, right;

Node(int num) {
this.num = num;
}

Node(int num, Node left, Node right) {
this(num);
this.left = left;
this.right = right;
}

void insert(int n) {
if (n < this.num) {
if (this.left == null)
this.left = new Node(n);
else
this.left.insert(n);
} else {
if (this.right == null)
this.right = new Node(n);
else
this.right.insert(n);
}
}
}

}
14 changes: 14 additions & 0 deletions Members/JongWon/Week12/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Members.JongWon.Week12;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws Exception {

}

}