-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopoSort.c
More file actions
59 lines (57 loc) · 1.46 KB
/
TopoSort.c
File metadata and controls
59 lines (57 loc) · 1.46 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdef _WIN32
#include<windows.h>
#endif
int adjacencyList[101][101];
int inDegree[101];
int n = 0;
int main(){
// Windows下设置UTF-8编码
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
DWORD mode;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(hConsole, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hConsole, mode);
#endif
memset(adjacencyList, 0, sizeof(adjacencyList));
memset(inDegree, 0, sizeof(inDegree));
printf("请输入无向图顶点个数(1~100): ");
scanf("%d", &n);
printf("请输入无向图的每条边,以\"0 0\"结尾:\n");
int a, b;
scanf("%d %d", &a, &b);
while(a != 0 && b != 0){
adjacencyList[a][0]++;
adjacencyList[a][adjacencyList[a][0]] = b;
inDegree[b]++;
scanf("%d %d", &a, &b);
}
for(int i = 0;i < n;i++){
int now = 0;
for(int j = 1;j <= n;j++){
if(inDegree[j] == 0){
now = j;
break;
}
}
if(now == 0){
printf("图中有环");
return 0;
} else{
printf("%d ", now);
inDegree[now] = -1;
for(int j = 1;j <= adjacencyList[now][0];j++){
inDegree[adjacencyList[now][j]]--;
}
}
}
printf("\n");
printf("按回车键退出\n");
getchar();
getchar();
return 0;
}