๋ฌธ์
ํ์ด
์ฒ์ ์๋ํ๋ ๋ฐฉ๋ฒ์ 1๋ฒ์ ์์๋ ธ๋๋ค์ ์์์ ์ผ๋ก ์ก์ DFS๋ฅผ ํตํด ํ์ํ๋ฉด์ ์ผ์ชฝ ์์๊ณผ์ ๊ฑฐ๋ฆฌ, ์ค๋ฅธ์ชฝ ์์๊ณผ์ ๊ฑฐ๋ฆฌ, ๋ถ๋ชจ์์ ๊ฑฐ๋ฆฌ ์ค์ ์์ 2๊ฐ์ ๊ฐ์ ๊ณจ๋ผ ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์งํํ์๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ์ต๋๊ฐ์ด 1๋ฒ์ ๋ค๋ฅธ ์ค๊ธฐ์ ์ฐ๊ฒฐํ ์ ์๋์ง(notCompleted), ์๋๋ฉด ์ด๋ฏธ ์์ฑ๋ ์ง๋ฆ์ธ์ง(completed) ํ๋ณํ๋ flag๋ฅผ ํตํด ์ต๋ ์ง๋ฆ์ ๊ตฌํ๋ ค๊ณ ํ๋ค.
์ด ๋ฐฉ๋ฒ์ด ์๋ชป๋ ์ด์ ๋ ๋จผ์ ๋ฌธ์ ์์ ์ด์ง ํธ๋ฆฌ๋ผ๋ ์กฐ๊ฑด์ด ์๋๋ฐ ์ด์งํธ๋ฆฌ๋ผ๊ณ ๊ฐ์ ํ๊ณ ํ์๊ธฐ ๋๋ฌธ์ด๊ณ , ๋ํ ์์ ๊ทธ๋ํ์์
4-7 : 100 / 4-8 : 100์ผ๋ก ์์ ํ ๊ฒฝ์ฐ ์ผ์ชฝ ์ค๊ธฐ์ ์ต๋๊ฐ์ด 200์ด ๋์์ผํ๋๋ฐ 4์ ๋ถ๋ชจ๋
ธ๋์ธ 2๊ฐ ์์์ด ํ๋๋ฐ์ ์๊ธฐ ๋๋ฌธ์ 200 + 5๋ก ๊ฐฑ์ ๋๋ ๋ฌธ์ ์ ์ด ์กด์ฌํ๋ค.
ํ๋ฆฐ ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค. (์ฌ์ค ํ๋ฉด์๋ ์ผ๋ฐํ์ํค์ง ๋ชปํ ์กฐ๊ฑด์ด ๋๋ฌด ๋ง์ ์๋ชป๋ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์์ง๋ง ๋
ผ๋ฆฌ์ ๋ฌธ์ ๋ฅผ ์ฐพ์ง ๋ชปํด์ ๊ทธ๋๋ก ์งํํ์๋ค.)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution1967 {
static class Node {
int dest;
int weight;
public Node(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N, maxLength;
static boolean isCompleted;
static ArrayList<Node>[] nodes;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
nodes = new ArrayList[N+1];
for (int i = 0; i < N-1; i++) {
String[] line = br.readLine().split(" ");
int idx = Integer.parseInt(line[0]);
if (nodes[idx] == null)
nodes[idx] = new ArrayList<>();
nodes[idx].add(new Node(Integer.parseInt(line[1]), Integer.parseInt(line[2])));
}
if (N == 1) {
System.out.println(0);
return;
}
ArrayList<Integer> completed = new ArrayList<>();
ArrayList<Integer> notCompleted = new ArrayList<>();
for(Node n : nodes[1]) {
isCompleted = false;
int len = dfs(n.dest, n.weight);
if (!isCompleted)
notCompleted.add(len);
else
completed.add(len);
}
completed.sort(Integer::compareTo);
notCompleted.sort(Integer::compareTo);
System.out.println(completed);
System.out.println(notCompleted);
int maxCompleted = 0, maxNotCompleted = 0;
if (completed.size() > 0)
maxCompleted = completed.get(completed.size() - 1);
if (notCompleted.size() > 0)
maxNotCompleted = notCompleted.get(notCompleted.size() - 1);
if (notCompleted.size() < 2)
System.out.println(Math.max(maxCompleted, maxNotCompleted));
else {
int secondNotCompleted = notCompleted.get(notCompleted.size() - 2);
System.out.println(Math.max(maxCompleted, maxNotCompleted + secondNotCompleted));
}
}
private static int dfs(int idx, int len) {
if (nodes[idx] == null)
return len;
int[] max = new int[2];
for (Node n : nodes[idx]) {
int curLen = dfs(n.dest, n.weight);
if (max[1] == 0)
max[1] = curLen;
else if (max[0] < curLen)
max[0] = curLen;
else if (max[1] < curLen)
max[1] = curLen;
}
if (max[0] > len && max[1] > len)
isCompleted = true;
else if (isCompleted )
return max[0] + max[1];
}
}
๊ทธ๋์ ๋ค์์ผ๋ก ์๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋จผ์ 1๋ฒ ๋ ธ๋์์ dfs๋ฅผ ์งํํด์ ๊ฐ์ฅ ๋จผ ์ (restartIdx)๋ฅผ ์ฐพ๊ณ ์ด ์ ์์ ๋ค์ ๊ฐ์ฅ ๋จผ ์ ์ ์ฐพ์ ํธ๋ฆฌ์ ์ต๋ ์ง๋ฆ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด์๋ค.
์ด ๋ฐฉ๋ฒ์ ์ํด์๋ ๋ฌธ์ ์์ ๋จ๋ฐฉํฅ์ด๋ผ๊ณ ๋งํ์ง๋ง ์๋ฐฉํฅ์ผ๋ก ์ฐ๊ด๊ด๊ณ๋ฅผ ์์ฑํด์ผ ํ๊ณ ์ฒซ ๋ฒ์งธ๋ก ์๊ฐํ๋ ๋ฐฉ๋ฒ์ ์ธ์ ํ๋ ฌ์ ํตํด ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด์๋ค. ํ์ง๋ง ์ธ์ ํ๋ ฌ์ ๊ฒฝ์ฐ ์ต๋ 10000 * 10000 ๊ฐ์ ๋ฐฐ์ด์ด ๋ง๋ค์ด์ง๊ณ ์ด๋ก ์ธํด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ์๋ค.
๋ ๋ฒ์งธ๋ก ์ ํํ ๋ฐฉ๋ฒ์ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ํตํด ๊ตฌํ๋ ๋ฐฉ๋ฒ์ผ๋ก src-dest : weight์ ๊ด๊ณ์ ์๋ค๋ฉด map[src]์ dest์ ์ ๋ณด๋ฅผ, map[dest]์ src์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์์ผ๋ก ์งํํ์๊ณ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
์ต์ข ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution1967 {
static class Node {
int idx;
int weight;
public Node(int idx, int dist) {
this.idx = idx;
this.weight = dist;
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N, maxLength, restartIdx;
static ArrayList<Node>[] nodes;
static boolean[] visited;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
nodes = new ArrayList[N+1];
for (int i = 0; i < N-1; i++) {
String[] line = br.readLine().split(" ");
int src = Integer.parseInt(line[0]);
int dest = Integer.parseInt(line[1]);
int weight =Integer.parseInt(line[2]);
if (nodes[src] == null)
nodes[src] = new ArrayList<>();
if (nodes[dest] == null)
nodes[dest] = new ArrayList<>();
nodes[src].add(new Node(dest, weight));
nodes[dest].add(new Node(src, weight));
}
if (N == 1) {
System.out.println(0);
return;
}
visited = new boolean[N+1];
dfs(1, 0);
maxLength = 0;
visited = new boolean[N+1];
dfs(restartIdx, 0);
System.out.println(maxLength);
}
private static void dfs(int src, int weight) {
if (visited[src]) return;
visited[src] = true;
if (maxLength < weight) {
maxLength = weight;
restartIdx = src;
}
for (int i = 0; i < nodes[src].size(); i++) {
if (visited[nodes[src].get(i).idx]) continue;
dfs(nodes[src].get(i).idx, weight + nodes[src].get(i).weight);
}
}
}
'๐ Algorithm > ๐ป ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] - 1277. ๋ฐ์ ์ ์ค์น (0) | 2021.03.24 |
---|---|
[๋ฐฑ์ค] 1799. ๋น์ (0) | 2021.03.07 |
[๋ฐฑ์ค] 1987. ์ํ๋ฒณ (0) | 2021.02.19 |
[๋ฐฑ์ค] 3109. ๋นต์ง (0) | 2021.02.19 |
[๋ฐฑ์ค] 15686. ์นํจ ๋ฐฐ๋ฌ (0) | 2021.02.19 |
๋๊ธ