회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
c언어 오류 찾아주세요
2250번 - 트리의 높이와 너비
practice430
2년 전
0
1프로에서 틀렸다고 뜨는데 어디가 틀렸는지 모르겠어요..
도와주세요!
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> struct tree { int index; int child_cnt; int depth; struct tree* up; struct tree* left; struct tree* right; struct tree* bro; int x; }; struct length { int min = 300005; int dex_min; int max = -300005; int dex_max; }; struct length arr[30005]; struct tree nodepool[30005]; struct tree* root; int nodepoolcnt = 0; int check[30005]; int que[30005]; int front = 0; int rear = 0; int n; int me, left, right; int deep = 0; void print() { for (int i = 1; i <= n; i++) { printf("%d노드 : ", i); if (nodepool[i].up != NULL) { printf("부모 : %d ", nodepool[i].up->index); } if (nodepool[i].left != NULL) { printf("왼 : %d ", nodepool[i].left->index); } if (nodepool[i].right != NULL) { printf("오 : %d ", nodepool[i].right->index); } printf("자식 개수 : %d, 깊이 : %d\n", nodepool[i].child_cnt, nodepool[i].depth); } } void init() { for (int i = 0; i <= n; i++) { check[i] = 0; } } void insert(int me, int left, int right) { nodepool[me].index = me; if (left != -1) { nodepool[me].left = &nodepool[left]; nodepool[left].up = &nodepool[me]; } if (right != -1) { nodepool[me].right = &nodepool[right]; nodepool[right].up = &nodepool[me]; } if (left != -1 && right != -1) { nodepool[left].bro = &nodepool[right]; } } void dfs(int dex, int depth) { check[dex] = 1; struct tree* cursor = &nodepool[dex]; cursor->child_cnt = 0; cursor->depth = depth; if (cursor->left == NULL) { cursor = cursor->right; } else { cursor = cursor->left; } while (cursor != NULL) { if (check[cursor->index] == 0) { deep = depth + 1; dfs(cursor->index, depth + 1); nodepool[dex].child_cnt += nodepool[cursor->index].child_cnt + 1; cursor = cursor->bro; } } } void bfs(int dex) { que[rear++] = dex; check[dex] = 1; while (front != rear) { int current = que[front++]; struct tree* cursor = &nodepool[current]; if (cursor->left != NULL && check[cursor->left->index] == 0) { que[rear++] = cursor->left->index; check[cursor->left->index] = 1; //좌표지정 if (cursor->left->right != NULL) { nodepool[cursor->left->index].x = nodepool[current].x - nodepool[cursor->left->right->index].child_cnt - 2; //printf("%d의 좌표 : %d\n", cursor->left->index, nodepool[cursor->left->index].x); } else { nodepool[cursor->left->index].x = nodepool[current].x - 1; //printf("%d의 좌표 : %d\n", cursor->left->index, nodepool[cursor->left->index].x); } if (arr[cursor->left->depth].min > nodepool[cursor->left->index].x) { arr[cursor->left->depth].min = nodepool[cursor->left->index].x; arr[cursor->left->depth].dex_min = cursor->left->index; } else if (arr[cursor->left->depth].max < nodepool[cursor->left->index].x) { arr[cursor->left->depth].max = nodepool[cursor->left->index].x; arr[cursor->left->depth].dex_max = cursor->left->index; } } if (cursor->right != NULL && check[cursor->right->index] == 0) { que[rear++] = cursor->right->index; check[cursor->right->index] = 1; //좌표지정 if (cursor->right->left != NULL) { nodepool[cursor->right->index].x = nodepool[current].x + nodepool[cursor->right->left->index].child_cnt + 2; //printf("%d의 좌표 : %d\n", cursor->right->index, nodepool[cursor->right->index].x); } else { nodepool[cursor->right->index].x = nodepool[current].x + 1; //printf("%d의 좌표 : %d\n", cursor->right->index, nodepool[cursor->right->index].x); } if (arr[cursor->right->depth].max < nodepool[cursor->right->index].x) { arr[cursor->right->depth].max = nodepool[cursor->right->index].x; arr[cursor->right->depth].dex_max = cursor->right->index; } else if (arr[cursor->right->depth].min > nodepool[cursor->right->index].x) { arr[cursor->right->depth].min = nodepool[cursor->right->index].x; arr[cursor->right->depth].dex_min = cursor->right->index; } } } } void print_xy() { for (int i = 1; i <= deep; i++) { printf("%d층: 최대 %d노드, 최소 %d노드, 너비 : %d\n", i, arr[i].dex_max, arr[i].dex_min, arr[i].max - arr[i].min); } } int dap; int max; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d %d", &me, &left, &right); insert(me, left, right); } for (int i = 1; i <= n; i++) { if (nodepool[i].up == NULL) { root = &nodepool[i]; root->x = 0; break; } } dfs(root->index, 0); init(); //printf("%d\n", deep); bfs(root->index); //print_xy(); for (int i = 1; i <= deep; i++) { if (max < arr[i].max - arr[i].min) { max = arr[i].max - arr[i].min; dap = i; } } //print(); printf("%d %d", dap + 1, max + 1); }
댓글을 작성하려면
로그인
해야 합니다.
practice430 2년 전
1프로에서 틀렸다고 뜨는데 어디가 틀렸는지 모르겠어요..
도와주세요!