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
47 changes: 47 additions & 0 deletions 12_트리/필수/15681.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 트리와 쿼리
#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 100005;
vector<int> tree[MAX_N]; // 트리 간선 정보 저장
int subtree_size[MAX_N]; // 각 노드의 서브트리 크기를 저장

// dfs 를 이용해 각 서브트리 크기 계산
void dfs(int node, int parent){
subtree_size[node] = 1; // 자기 자신을 포함하므로 초기값은 1
for (int neighbor : tree[node]) {
if (neighbor != parent) { // 부모 노드로 돌아가지 않음
dfs(neighbor, node);
subtree_size[node] += subtree_size[neighbor];
}
}
}

int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

int n, r, q; // 트리의 정점의 수 n과 루트의 번호 r, 쿼리의 수 q
int u, v;

// 입력
cin >> n >> r >> q;
for (int i = 0; i < n - 1; i++){
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}

// 서브트리 크기 계산
dfs(r, -1);

// 쿼리 입력, 연산, 출력
for (int i = 0; i < q; i++){
int input;
cin >> input;
cout << subtree_size[input] << '\n';
}

return 0;
}
91 changes: 91 additions & 0 deletions 12_트리/필수/3190.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 뱀
#include <iostream>
#include <vector>
#include <deque>
#include <map>
using namespace std;

// 방향 벡터 (동, 남, 서, 북 순서)
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

//
int runGame(int n, vector<vector<int>> &board, deque<pair<int, int>> &snake, map<int, char> &directions){
int time = 0, dir = 0; // 시간, 초기 방향(동쪽)

while (true) {
++time;

// 현재 머리 위치와 새 위치 계산
int x = snake.front().first;
int y = snake.front().second;
int nx = x + dx[dir];
int ny = y + dy[dir];

// 벽이나 자기 몸과 부딪혔는지 확인
if (nx <= 0 || ny <= 0 || nx > n || ny > n || board[nx][ny] == 2) {
return time; // 게임 종료
}

// 이동한 칸에 사과가 있는 경우
if (board[nx][ny] == 1) {
board[nx][ny] = 2; // 머리 위치 업데이트
snake.push_front({nx, ny});
}
// 이동한 칸에 사과가 없는 경우
else {
board[nx][ny] = 2; // 머리 위치 업데이트
snake.push_front({nx, ny});
auto tail = snake.back(); // 꼬리 위치
snake.pop_back();
board[tail.first][tail.second] = 0; // 꼬리 위치 비우기
}

// 방향 변환 처리
if (directions.count(time)) {
char turn = directions[time];
if (turn == 'L') {
dir = (dir + 3) % 4; // 왼쪽 회전
} else if (turn == 'D') {
dir = (dir + 1) % 4; // 오른쪽 회전
}
}
}
}

int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

int n, k; // n: 보드의 크기 (2 이상 100 이하), k: 사과의 개수 (0 이상 100 이하)
int l; // l: 뱀의 방향 변환 횟수

// 보드 초기화
cin >> n >> k;
vector<vector<int>> board(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i < k; ++i) {
int r, c;
cin >> r >> c;
board[r][c] = 1; // 사과 위치한 칸은 1로 표시
}

// 방향 변환 정보 입력
cin >> l;
map<int, char> directions;
for (int i = 0; i < l; ++i) {
int x;
char c;
cin >> x >> c;
directions[x] = c;
}

// 뱀 초기화
deque<pair<int, int>> snake;
snake.push_back({1, 1});
board[1][1] = 2; // 뱀이 위치한 칸은 2로 표시

// 게임 실행 및 결과 출력
cout << runGame(n, board, snake, directions) << '\n';

return 0;
}
45 changes: 45 additions & 0 deletions 12_트리/필수/5639.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 이진 검색 트리
#include <iostream>
#include <vector>
using namespace std;

// 후위 순회하고 출력하는 함수
void postorder(const vector<int> &preorder, int start, int end){
if (start > end) return;

int root = preorder[start];
int right_start = start + 1;

// 오른쪽 서브트리의 시작 위치 찾기
// right_start 는 (preorder[i] > root 인 순간의 i) 이다.
while (right_start <= end && preorder[right_start] < root) {
right_start++;
}

// 왼쪽 서브트리
postorder(preorder, start + 1, right_start - 1);

// 오른쪽 서브트리
postorder(preorder, right_start, end);

// 루트 노드 출력
cout << root << '\n';
}

int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

int input;
vector<int> preorder;

// 입력 (전위 순회)
while (cin >> input){
preorder.push_back(input);
}

// 후위 순회 연산 및 출력
postorder(preorder, 0, preorder.size() - 1);

return 0;
}