-
Notifications
You must be signed in to change notification settings - Fork 0
/
281.c
104 lines (88 loc) · 2.11 KB
/
281.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// http://www.bjfuacm.com/problem/281/
#include <stdio.h>
#include <stdlib.h>
#define MAX_INT 32767
#define MVNum 100
typedef int VerTexType;
typedef int ArcType;
struct AMGraph;
typedef struct AMGraph AMGraph;
struct AMGraph {
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum;
int arcnum;
};
int LocateVex(AMGraph * , VerTexType );
AMGraph * CreateUDN(AMGraph * );
void DeleteArc(AMGraph * );
void PrintGraph(AMGraph * );
void DestroyGraph(AMGraph * );
int main()
{
while (1) {
AMGraph *G = (AMGraph *)calloc(1, sizeof(*G));
G = CreateUDN(G);
if (G == NULL) break;
DeleteArc(G);
PrintGraph(G);
DestroyGraph(G);
}
}
int LocateVex(AMGraph *G, VerTexType vex)
{
for (int i = 1; i <= G->vexnum; i++) {
if (vex == G->vexs[i]) return i;
}
}
AMGraph * CreateUDN(AMGraph *G)
{
int v1; // node 1, is a name
int v2; // node 2, is also a name
scanf("%d %d", &(G->vexnum), &(G->arcnum));
if (G->vexnum == 0 && G->vexnum == 0) return NULL;
for (int i = 1; i <= G->vexnum; i++) {
G->vexs[i] = i;
}
for (int i = 1; i <= G->vexnum; i++) {
for (int j = 1; j <= G->vexnum; j++) {
G->arcs[i][j] = 0; // no connection here
}
}
int i; // index of arch_1
int j; // index of arch_2
for (int k = 1; k <= G->arcnum; k++) {
scanf("%d %d", &v1, &v2);
i = LocateVex(G, v1);
j = LocateVex(G, v2);
G->arcs[i][j] = 1;
G->arcs[j][i] = 1;
}
return G;
}
void DeleteArc(AMGraph *G)
{
int v1;
int v2;
scanf("%d %d", &v1, &v2);
int i = LocateVex(G, v1);
int j = LocateVex(G, v2);
G->arcs[i][j] = 0;
G->arcs[j][i] = 0;
}
void PrintGraph(AMGraph *G)
{
for (int i = 0; i <= G->vexnum; i++) {
printf("%d%s", G->vexs[i], i == G->vexnum? "\n": " ");
}
for (int i = 1; i <= G->vexnum; i++) {
printf("%d ", G->vexs[i]);
for (int j = 1; j <= G->vexnum; j++) {
printf("%d%s", G->arcs[i][j], j == G->vexnum? "\n": " ");
}
}
}
void DestroyGraph(AMGraph *G)
{
free(G);
}