본문 바로가기
Computer Science

A응실 작업순서 / 위상정렬

by OKOK 2018. 12. 19.

1. 위상정렬 풀이

2. 벌텍스 어레이

3. 포인터 어레이 만들어서 풀이함

4. 에지 넣기

5. 풀어나가기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define _CRT_SECURE_NO_WARNINGS
//#define MAX 1001
#define MAX 10
#include <stdio.h>
 
typedef struct vertex {
    int num;            //정점번호
    struct vertex* next;//다음노드
}Vertex;
 
Vertex* arr[MAX];
int V, E;
int preEdge[MAX];
bool check[MAX];
 
void insertEdge(int a, int b)
{
    Vertex* newVertex = new Vertex;
    newVertex->num = b;
    newVertex->next = NULL;
 
 
    if (arr[a] == NULL)
        arr[a] = newVertex;
    else {
        newVertex->next = arr[a];
        arr[a] = newVertex;
    }
}
 
void sol(int n)
{
    Vertex* cur = arr[n];
    while (cur != NULL)
    {
        preEdge[cur->num]--;
        if (!check[cur->num] && preEdge[cur->num] == 0) {
            printf(" %d", cur->num);
            check[cur->num] = true;
            preEdge[cur->num]--;
            sol(cur->num);
        }
        cur = cur->next;
 
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    for (int t = 1; t <= 10; t++)
    {
        scanf("%d%d"&V, &E);
        for (int i = 0; i<E; i++)
        {
            int a, b;
            scanf("%d%d"&a, &b);
            insertEdge(a, b);
            preEdge[b]++;
        }
        printf("#%d", t);
        for (int i = 1; i <= V; i++)
        {
            if (preEdge[i] == 0) {
                printf(" %d", i);
                sol(i);
            }
        }
        printf("\n");
 
        for (int i = 1; i <= V; i++)
        {
            check[i] = false;
            preEdge[i] = 0;
            arr[i] = NULL;
        }
 
    }
}
 
cs


댓글