회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
실패가 뜨는데 실패 테스트 케이스가 궁금해요
25973번 - 어지러운 트리
aoml3245
3년 전
0
손으로 만든 트리에서는 잘 되는데 1%부터 틀렷다고 나오네요..
#include <stdio.h> #include <stdlib.h> typedef struct Link { int node_num; int of_direct_child_n; struct Link *next; }link; typedef struct Node { link * connected; int connected_node_n; int depth; int up_direct_to_center; } node; void free_link(link* l){ if(l){ free_link((*l).next); } free(l); return; } void init_visited(int *visited, int n){ int i; for(i=0 ; i<n+1;i++){ visited[i] = 0; } } void cal_depth(node *graph, int *visited, int n){ int stack[2][300000]; int stack_read_i=0,stack_write_i=0, i, pop_i, depth=1, stack_read=0, stack_write=1; link *l=NULL; for(i=0;i<n+1;i++){ if(graph[i].connected_node_n == 1){ l = graph[i].connected; while(l){ stack[stack_write][++stack_write_i] = (*l).node_num; l =(*l).next; } visited[i] = 1; graph[i].depth = depth; } } stack_write = !stack_write; stack_read = !stack_read; stack_read_i = stack_write_i; stack_write_i = 0; while (stack_read_i != 0) { depth++; for(i=1;i<=stack_read_i;i++){ pop_i =stack[stack_read][i]; if(visited[pop_i] == 1) continue; else{ visited[pop_i] = 1; l = graph[pop_i].connected; while(l){ stack[stack_write][++stack_write_i] = (*l).node_num; l =(*l).next; } graph[pop_i].depth = depth; } } stack_write = !stack_write; stack_read = !stack_read; stack_read_i = stack_write_i; stack_write_i = 0; } return; } int find_center(node *graph, int n){ int i, max=0, center=1; for(i=0; i<=n;i++){ if(max < graph[i].depth){ center = i; max =graph[i].depth; } } return center; }; int set_DirectChildN_UpDirectToCenter(node *graph, int *visited, int i, int n, int from, int center){ link *l=NULL, * to_l=NULL; int child_sum = 0, child_n; if(graph[i].depth == 1 && center != i){ (*(graph[i].connected)).of_direct_child_n = n - 1; visited[i] = 1; graph[i].up_direct_to_center = from; return 1; } l = graph[i].connected; while(l){ if((*l).node_num == from){ to_l = l; } else{ child_n = set_DirectChildN_UpDirectToCenter(graph, visited, (*l).node_num, n, i, center); (*l).of_direct_child_n = child_n; child_sum += child_n; } l =(*l).next; } if(to_l){ (*to_l).of_direct_child_n = n - 1 - child_sum; } graph[i].up_direct_to_center = from; return child_sum + 1; } int find_up_direct_to_root(node * graph, int center, int root, int node_i){ if(root == node_i) return node_i; if(root == center){ return graph[node_i].up_direct_to_center;; } while(1){ //올라가다 만난 경우 if(graph[root].up_direct_to_center == node_i) return root; //depth가 더 커져 더이상 볼 필요없을 때 if(graph[node_i].depth <= graph[root].depth) return graph[node_i].up_direct_to_center; //올라갈 곳이 더이상 없을 때, 안만날 때 if(root == center) return graph[node_i].up_direct_to_center; //올라가기 root = graph[root].up_direct_to_center; } return node_i; } int cal_LCA(node* graph, int up_direct, int root, int node_x, int n){ link *l=NULL; int remain_node = 0, LCA_sum=0, all_child=0; if(root== node_x){ remain_node = n-1; } else{ l = graph[node_x].connected; while(l){ if((*l).node_num == up_direct){ remain_node = n - (*l).of_direct_child_n - 1; break; } l =(*l).next; } } l = graph[node_x].connected; while(l){ if((*l).node_num != up_direct){ LCA_sum += (remain_node - (*l).of_direct_child_n) * (*l).of_direct_child_n; remain_node -= (*l).of_direct_child_n; all_child += (*l).of_direct_child_n; } l =(*l).next; } return LCA_sum + all_child; } int main(int argc, const char * argv[]) { int n, q, i, a, b, center, root=1, query=0, x=0, up_direct=0, j; node graph[200001]; int visited[200001]; link * link_temp; scanf("%d %d", &n, &q); for(i=0 ; i<=n+1;i++){ graph[i].connected = NULL; graph[i].connected_node_n = 0; graph[i].depth = 0; } for(i=0 ; i<n-1;i++){ scanf("%d %d", &a, &b); link_temp = (link*)malloc(sizeof(link)); (*link_temp).next = graph[a].connected; (*link_temp).node_num = b; graph[a].connected = link_temp; graph[a].connected_node_n++; link_temp = (link*)malloc(sizeof(link)); (*link_temp).next = graph[b].connected; (*link_temp).node_num = a; graph[b].connected = link_temp; graph[b].connected_node_n++; } init_visited(visited, n); cal_depth(graph, visited, n); center = find_center(graph, n); init_visited(visited, n); set_DirectChildN_UpDirectToCenter(graph, visited, center, n, center, center); init_visited(visited, n); for(i=0;i<q;i++){ scanf("%d %d", &query, &x); if(query == 1) { root = x; } else{ up_direct = find_up_direct_to_root(graph, center, root, x); printf("%d\n", cal_LCA(graph, up_direct, root, x, n)); } } for(i=0 ; i<n+1;i++){ free_link(graph[i].connected); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
aoml3245 3년 전
손으로 만든 트리에서는 잘 되는데 1%부터 틀렷다고 나오네요..