회원가입
로그인
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
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
11438 11퍼정도에서 자꾸 실패하네요 ㅠㅠ
11438번 - LCA 2
juneodie
4년 전
0
여러글들 다 참고해보았는데 왜 틀렸는지 아직도 모르겠네요 ㅠㅠ
도와주시면 감사하겠습니다...
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; vector<vector<int>> vec; int parent[100010][18]; int Height(int where,int count) { int i; int height = 0; int n=0; while ( 1 ) { for (i=0; i<count; i++) { if( parent[where][i] == 0 ) break; } i-- ; if( i<0 ) break; height += pow(2,i); where = parent[where][i]; } return height; } int main() { cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false); int N,M; int i,j,k; int abc=0; int binary[100]; int binarycount=0; int input1,input2; int square2; int count=0; int tmp; int breakpoint=0; cin >> N; square2 = N; while( square2 > 1 ) { square2 /= 2; count ++; } vec.assign (N+1,vector<int>(0,0)); for(i=0;i<N+1;i++) { for(j=0;j<count+1;j++) { parent[i][j] = 0; } } // parent 0으로 초기화 for(i=0;i<N-1;i++) { cin >> input1 >> input2; if( input1 > input2 ) { tmp = input1; input1 = input2; input2 = tmp; } // input1 이 더 작은수가 되게 바꿔준다 vec[input1].push_back(input2); // 자기 아래번호들을 저장 } for(j=1;j<N+1;j++) { for(k=0 ; k< vec[j].size(); k++) { parent[ vec[j][k] ][0] = j; } } // parent 첫번째 만들기 for(i=1; i<count+1; i++) { for(j=1;j<N+1;j++) { parent[j][i] = parent[ parent[j][i-1] ] [ i-1 ]; } } // parent 나머지 저장; cin >> M; for(i=0; i<M; i++) { cin >> input1 >> input2; if ( Height(input1,count+1) > Height(input2,count+1) ) { tmp = input1; input1 = input2; input2 = tmp; } // input1 이 높이가 더 작게 해준다; int gap = abs(Height(input2,count+1) - Height(input1,count+1)); binarycount = 0; while( 1 ) { if(gap<1) break; binary[binarycount] = gap%2; binarycount++; gap /= 2; } for(j=0; j<binarycount; j++) { if( binary[j] == 1 ) { input2 = parent[input2][j]; } } //높이를 맞춰준다 while (1) { for (j=0; j<count+1; j++) { if( parent[input1][j] == 0 ) break; } if(input1 == input2) break; else if(parent[input1][0] == parent[input2][0] ) { input1 = parent[input1][0]; break; } for (k=1; k<count+1; k++) { if( parent[input1][k] == parent[input2][k] ) { input1 = parent[input1][k-1]; input2 = parent[input2][k-1]; break; } } } // 높이가 같은 상황에서 공통 조상 구하기 cout << input1<<"\n"; } }
댓글을 작성하려면
로그인
해야 합니다.
juneodie 4년 전
여러글들 다 참고해보았는데 왜 틀렸는지 아직도 모르겠네요 ㅠㅠ
도와주시면 감사하겠습니다...