top of page

소스 코드 제출

공개·회원 52명

250814

#include<stdio.h>


int maxIdx(int *arr);

void dfs(int nextNode, int f1);



int tree[2000][20] = {0};

int visited[1000] = {0};

int foundParents[2000][15] = {{0}};


int main() {


int n, i,j,k, n1,n2, m;

scanf("%d", &n);

for (i=0; i<n-1; i++) {

scanf("%d %d", &n1, &n2);

tree[n1][maxIdx(tree[n1])] = n2;

tree[n2][maxIdx(tree[n2])] = n1;

}

scanf("%d", &m);

for (i=0; i<m; i++) {

scanf("%d %d", &n1, &n2);

dfs(1, n1);

for (j=0; j<n-1; j++) {

visited[i]=0;

}

dfs(1, n2);

for (j=0; j<n-1; j++) {

visited[i]=0;

}


for (j=maxIdx(foundParents[n1])-1; j>=0; j--) {

for (k=maxIdx(foundParents[n2])-1; k>=0; k--) {

// printf("%d %d\n",foundParents[n1][j], foundParents[n2][k] );

if (foundParents[n1][j]==foundParents[n2][k]) {

printf("answer: %d\n", foundParents[n1][j]);

j=-1;

k=-1;

}

}

}


}



return 0;

}


// 0인 곳. 빈 리스트 인덱스를 리턴해주는 함수

int maxIdx(int *arr) {


int m=0;

while (arr[m]!=0) {

m++;

}

return m;

}


void dfs(int nextNode, int f) {

foundParents[f][maxIdx(foundParents[f])] = nextNode;

printf("n: %d idx: %d v: %d\n",f, maxIdx(foundParents[f])-1, foundParents[f][maxIdx(foundParents[f])-1]);

visited[nextNode] = 1;

if (nextNode==f) {

return;

}

for (int l=0; l<maxIdx(tree[nextNode]); l++) {

if (visited[tree[nextNode][l]]!= 1 ) {

dfs(tree[nextNode][l],f);

}

}


}

6회 조회
주소 : 경기도 용인시 광교중앙로 302 블루 스퀘어 602호
연락처 : 031) 216 - 1546 ,     031) 215 - 1546
사업자등록번호 : 465-92-00916
​학원 등록 제 4603호
bottom of page