회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
대체 어디가 틀린건지 모르겠어요.. 테스트케이스는 맞는 것 같은데
24479번 - 알고리즘 수업 - 깊이 우선 탐색 1
gkgkdi12
2년 전
0
대체 어디가 틀린건지 모르겠어요.. 테스트케이스는 맞는 것 같은데..
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_STACK 1000000 #define MAX_QUEUE 1000000 typedef struct { int items[MAX_STACK]; int top; } Stack; typedef struct { int front, rear; int items[MAX_QUEUE]; } Queue; void InitStack(Stack* pstack) { pstack->top = -1; } int IsSFull(Stack* pstack) { return pstack->top == MAX_STACK - 1; } int IsSEmpty(Stack* pstack) { return pstack->top == -1; } int SPeek(Stack* pstack) { if (IsSEmpty(pstack)) exit(1); //error: empty stack return pstack->items[pstack->top]; } void Push(Stack* pstack, int item) { if (IsSFull(pstack)) exit(1); //error: stack full pstack->items[++(pstack->top)] = item; } int Pop(Stack* pstack) { if (IsSEmpty(pstack)) exit(1); //error: empty stack int item = pstack->items[pstack->top]; --(pstack->top); return item; } void InitQueue(Queue* pqueue) { pqueue->front = pqueue->rear = 0; } int IsQFull(Queue* pqueue) { return pqueue->front == (pqueue->rear + 1) % MAX_QUEUE; } int IsQEmpty(Queue* pqueue) { return pqueue->front == pqueue->rear; } int QPeek(Queue* pqueue) { if (IsQEmpty(pqueue)) exit(1); //error: empty stack return pqueue->items[pqueue->front]; } void EnQueue(Queue* pqueue, int item) { if (IsQFull(pqueue)) exit(1); //error: stack full pqueue->items[pqueue->rear] = item; pqueue->rear = (pqueue->rear + 1) % MAX_QUEUE; } int DeQueue(Queue* pqueue) { if (IsQEmpty(pqueue))exit(1); int item=pqueue->items[pqueue->front]; pqueue->front=(pqueue->front+1) % MAX_QUEUE; return item; } #define MAX_SIZE 100 typedef enum {false, true} bool; typedef struct _GNode { int id; int seq; struct _GNode *next; }GNode; typedef struct { int num; GNode **heads; } Graph; void CreateGraph(Graph *pgraph, int num); void DestroyGraph(Graph *pgraph); void AddEdge(Graph *pgraph, int src, int dest); void PrintGraph(Graph *pgraph); void DFS(Graph *pgraph, int startV); void BFS(Graph *pgraph, int startV); void GetInput(); /* Modify from here */ int main() { Graph g; int numofnode,numofedge, startV; scanf("%d %d %d",&numofnode,&numofedge,&startV); CreateGraph(&g, numofnode); int num1,num2; for(int i=0;i<numofedge;i++) { scanf("%d %d",&num1,&num2); AddEdge(&g,num1,num2); } DFS(&g,startV); DestroyGraph(&g); return 0; } void CreateGraph(Graph *pgraph, int num) { pgraph->heads=(GNode**)malloc(sizeof(GNode*)*num); pgraph->num=num; for(int i=0;i<num;i++) { pgraph->heads[i]=(GNode *)malloc(sizeof(GNode)); pgraph->heads[i]->id=i+1; pgraph->heads[i]->seq=0; pgraph->heads[i]->next=NULL; } } void DestroyGraph(Graph *pgraph) { for(int i=0;i<pgraph->num;i++) { GNode *cur=pgraph->heads[i]; while(cur!=NULL) { GNode *tmp=cur; cur=cur->next; free(tmp); } } } void AddEdge(Graph *pgraph, int src, int dest) { GNode *newNode1, *newNode2, *cur; newNode1=(GNode *)malloc(sizeof(GNode)); newNode1->id=dest; newNode1->next=NULL; cur=pgraph->heads[src-1]; if(cur->next==NULL)cur->next=newNode1; else if(dest > cur->next->id) { newNode1->next=cur->next; cur->next=newNode1; } else { while(cur!=NULL) { if(cur->next==NULL) { cur->next=newNode1; break; } else if(cur->id > dest && cur->next->id < dest) { newNode1->next=cur->next; cur->next=newNode1; break; } cur=cur->next; } } newNode2=(GNode *)malloc(sizeof(GNode)); newNode2->id=src; newNode2->next=NULL; cur=pgraph->heads[dest-1]; if(cur->next==NULL)cur->next=newNode2; else if(dest > cur->next->id) { newNode2->next=cur->next; cur->next=newNode2; } else { while(cur!=NULL) { if(cur->next==NULL) { cur->next=newNode2; break; } else if(cur->id > src && cur->next->id < src) { newNode2->next=cur->next; cur->next=newNode2; break; } cur=cur->next; } } } void DFS(Graph *pgraph, int startV) { bool *visited=(bool *)malloc(sizeof(bool)*pgraph->num); for(int i=0;i<pgraph->num;i++)visited[i]=false; Stack stack; InitStack(&stack); Push(&stack,startV); int seq=1; while(!IsSEmpty(&stack)) { GNode *cur; int vx=SPeek(&stack); Pop(&stack); if(visited[vx-1])continue; else { visited[vx-1]=true; pgraph->heads[vx-1]->seq=seq; seq++; } cur=pgraph->heads[vx-1]->next; while(cur!=NULL) { if(!visited[cur->id-1])Push(&stack,cur->id); cur=cur->next; } } for(int i=0;i<pgraph->num;i++) { printf("%d\n",pgraph->heads[i]->seq); } }
댓글을 작성하려면
로그인
해야 합니다.
gkgkdi12 2년 전
대체 어디가 틀린건지 모르겠어요.. 테스트케이스는 맞는 것 같은데..