SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
vector<pair<int, int>> tree(N + 1);
for(int i = 1; i <= N; ++i) {
int a, b, c; cin >> a >> b >> c;
tree[a] = make_pair(b, c);
}
int ans = 2 * (N - 1);
for(int cur = 1; tree[cur].second != -1; cur = tree[cur].second) {
-- ans;
}
cout << ans;
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
잘 생각해보면 가장 루트에서 시작하여 가장 오른쪽으로 가는 경로를 제외하고는 2번 이동한다.
직접 유사 중위 순회를 구현해도 되지만, 잘 관찰을 하면 답은 2 * (N - 1) - (루트에서 시작해서 가장 오른쪽 노드까지 이동하는 횟수)가 되는 것을 알 수 있다.
SOLUTION INFO
Java · Main.java
- 작성자
- suin8
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
class Node {
public int left, right, parent;
Node(){
this.left = -1;
this.right = -1;
this.parent = -1;
}
void setChildren(int left, int right) {
this.left = left;
this.right = right;
}
void setParent(int parent) {
this.parent = parent;
}
}
public class Main {
static Node node[];
static int count, N, end;
static boolean visited[];
public static void main(String[] args) {
FastReader rd = new FastReader();
N = rd.nextInt();
node = new Node[N + 10];
visited = new boolean[N + 10];
for(int i = 1;i <= N;i++)
node[i] = new Node();
for(int i = 1;i <= N;i++) {
int cur = rd.nextInt();
int left = rd.nextInt();
int right = rd.nextInt();
node[cur].setChildren(left, right);
if(left != -1) node[left].setParent(cur);
if(right != -1) node[right].setParent(cur);
}
find_end(1);
recur(1);
// 구현 상 지나온 노드수를 세기 때문에
// 루트노드인 1도 세어져서 루트 1개를 빼준다
System.out.println(count - 1);
}
// 순회의 끝 찾기. 항상 가장 오른쪽 노드가 순회의 끝
static void find_end(int cur) {
end = cur;
if(node[cur].right != -1)
find_end(node[cur].right);
else return;
}
// 유사 중위 순회
static void recur(int cur) {
count++;
visited[cur] = true;
// 왼쪽 미방문
if(node[cur].left != -1 && visited[node[cur].left] == false)
recur(node[cur].left);
// 왼쪽 방문, 오른쪽 미방문
else if(node[cur].right != -1 && visited[node[cur].right] == false)
recur(node[cur].right);
// 왼쪽 방문, 오른쪽 방문, 순회의 끝일 때
else if(cur == end) return;
// 왼쪽 방문, 오른쪽 방문, 순회의 끝이 아닐 때
else recur(node[cur].parent);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
JavaScript · main.js
- 작성자
- tony9402
- 공동 작성자
- 없음
class InputModule {
constructor(trim = false) {
this.buffer = require("fs").readFileSync("/dev/stdin").toString();
if(trim) this.buffer = this.buffer.trim();
this.buffer = this.buffer.split("\n").map(x => x.split(" "));
this.x = 0;
this.y = 0;
this.pointer = 0;
}
_nextPointer() {
if(this.y === this.buffer.length) return;
if(this.x === this.buffer[this.y].length) {
this.x = 0;
if(++ this.y == this.buffer.length) return;
}
while(this.y < this.buffer.length && this.x === this.buffer[this.y].length) {
this.x = 0;
this.y ++;
}
}
_read() {
if(this.y === this.buffer.length) return null;
const ret = this.buffer[this.y][this.x][this.pointer ++];
if(this.pointer === this.buffer[this.y][this.x].length) {
this.x ++;
this.pointer = 0;
}
this._nextPointer();
return ret;
}
ignore() {
this.x ++;
this.pointer = 0;
this._nextPointer();
}
readChar() {
return this._read();
}
readString() {
if(this.y === this.buffer.length) return null;
const ret = this.buffer[this.y][this.x];
this.pointer = 0;
this.x ++;
this._nextPointer();
return ret;
}
readLine() {
if(this.y === this.buffer.length) return null;
const ret = this.buffer[this.y].join(' ');
this.y ++;
this.x = 0;
this.pointer = 0;
this._nextPointer();
return ret;
}
readInt() {
return Number.parseInt(this.readString());
}
readBigInt() {
return BigInt(this.readString());
}
readFloat() {
return Number.parseFloat(this.readString());
}
}
class OutputModule {
constructor() {
this.bufferMaxSize = 100000;
this.buffer = new Array(this.bufferMaxSize);
this.pointer = 0;
}
write(x) {
if(this.pointer === this.bufferMaxSize) {
this.flush();
}
this.buffer[this.pointer ++] = x;
}
flush() {
if(this.pointer > 0) {
process.stdout.write(this.buffer.slice(0, this.pointer).join(""));
this.pointer = 0;
}
}
}
const input = new InputModule();
const output = new OutputModule();
const N = input.readInt();
const nextRight = new Array(N + 1);
for(let i = 1; i <= N; ++i) {
const a = input.readInt();
const b = input.readInt();
const c = input.readInt();
nextRight[a] = c;
}
var ans = 2 * (N - 1);
for(let cur = 1; nextRight[cur] !== -1; cur = nextRight[cur]) {
ans --;
}
output.write(ans);
output.flush();
SOLUTION DESCRIPTION
풀이 설명
잘 생각해보면 가장 루트에서 시작하여 가장 오른쪽으로 가는 경로를 제외하고는 2번 이동한다.
직접 유사 중위 순회를 구현해도 되지만, 잘 관찰을 하면 답은 2 * (N - 1) - (루트에서 시작해서 가장 오른쪽 노드까지 이동하는 횟수)가 되는 것을 알 수 있다.