-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_adjacency_martix.cpp
More file actions
158 lines (135 loc) · 3.09 KB
/
Copy pathgraph_adjacency_martix.cpp
File metadata and controls
158 lines (135 loc) · 3.09 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* graph_adjacency_martix
*/
#include <iostream>
#include <string>
#include <vector>
#define M 500
using namespace std;
struct Vertex {
int item;
int degree;
Vertex() {}
Vertex(int item) : item(item), degree(0) {}
void increaseDegree() {
degree++;
}
void decreaseDegree() {
degree--;
}
};
struct Edge {
Vertex *src;
Vertex *dst;
string item;
Edge() {}
Edge(Vertex *src, Vertex *dst) : src(src), dst(dst) {}
void setEdgeItem(string name) {
item = name;
}
};
class Graph {
private:
int v_sz;
int max_sz;
int cnt;
Vertex **v;
vector<Edge *> e;
Edge ***mat;
public:
Graph() {
v_sz = 0;
max_sz = M;
cnt = 0;
v = new Vertex *[M];
for (int i = 0; i < M; i++) {
v[i] = NULL;
}
mat = new Edge **[M];
for (int i = 0; i < M; i++) {
mat[i] = new Edge *[M];
for (int j = 0; j < M; j++) {
mat[i][j] = NULL;
}
}
}
void insert(int data) {
if (v_sz + 1 > max_sz) {
cout << 0 << endl;
return;
}
if (v[data] != NULL) {
cout << 0 << endl;
return;
}
v[data] = new Vertex(data);
v_sz++;
}
void insertEdge(int src, int dst) {
if (v[src] == NULL || v[dst] == NULL) {
cout << 0 << endl;
return;
}
if (mat[src][dst] != NULL || mat[dst][src] != NULL) {
cout << 0 << endl;
return;
}
Edge *newEdge = new Edge(v[src], v[dst]);
e.push_back(newEdge);
mat[src][dst] = newEdge;
mat[dst][src] = newEdge;
v[src]->increaseDegree();
v[dst]->increaseDegree();
}
void removeEdge(int src, int dst) {
if (mat[src][dst] == NULL || mat[dst][src] == NULL) {
return;
}
if (v[src] == NULL || v[dst] == NULL) {
return;
}
for (int i = 0; i < (int) e.size(); i++) {
if (e[i] == mat[src][dst] || e[i] == mat[dst][src]) {
e.erase(e.begin() + i);
mat[src][dst] = NULL;
mat[dst][src] = NULL;
v[src]->decreaseDegree();
v[dst]->decreaseDegree();
}
}
}
void remove(int data) {
if (v[data] == NULL) {
cout << -1 << endl;
return;
}
for (int i = 0; i < M; i++) {
removeEdge(data, i);
}
v[data] = NULL;
v_sz--;
show();
}
void show() {
cout << v_sz << " " << e.size() << endl;
}
};
int main() {
Graph graph;
int m, n, o, input1, input2;
cin >> m >> n >> o;
for (int i = 0; i < m; i++) {
cin >> input1;
graph.insert(input1);
}
for (int i = 0; i < n; i++) {
cin >> input1 >> input2;
graph.insertEdge(input1, input2);
}
for (int i = 0; i < o; i++) {
cin >> input1;
graph.remove(input1);
}
system("pause");
return 0;
}