-
Notifications
You must be signed in to change notification settings - Fork 0
/
1233.cpp
142 lines (128 loc) · 2.98 KB
/
1233.cpp
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
#include <iostream>
using namespace std;
class adjListGraph{
private:
int Vers, Edges;
struct edgeNode{
int end_index;
int weight;
edgeNode *next;
edgeNode(int e, int w, edgeNode *n = NULL)
{
end_index = e;
weight = w;
next= n;
}
};
struct verNode{
int ver;
edgeNode *head;
verNode(edgeNode *h = NULL){
head = h;
}
};
verNode *verList;
public:
adjListGraph(int vSize){
Vers = vSize;
Edges = 0;
verList = new verNode[vSize+1];
for(int i=0;i<=Vers;++i){
verList[i].ver = i;
}
}
bool insert(int u, int v, int w = 1){
verList[u].head = new edgeNode(v,w,verList[u].head);
++Edges;
return true;
}
bool remove(int u, int v){
edgeNode *p = verList[u].head, *q;
if(p==NULL)
return false;
if(p->end_index == v){
verList[u].head = p->next;
delete p;
--Edges;
return true;
}
while(p->next !=NULL)
{
if(p->next->end_index == v){
q = p->next;
p->next = p->next->next;
delete q;
--Edges;
return true;
}
p = p->next;
}
return false;
}
bool exist(int u, int v)const {
edgeNode *p = verList[u].head;
while(p!=NULL)
{
if(p->end_index == v)
return true;
p = p->next;
}
return false;
}
int find(int start , int m) const{
int num = 0;
bool *visited = new bool [Vers+1];
int *stack = new int [m+1];
int top_index = 0;
for(int i=0;i<=Vers;++i){
visited[i] = false;
}
find(start,m,top_index,visited, stack, num);
return num;
}
~adjListGraph(){
edgeNode *p;
for(int i=0;i<=Vers;++i)
while((p = verList[i].head)!=NULL){
verList[i].head=p->next;
delete p;
}
delete [] verList;
}
private:
void find(int start, int m, int &top, bool visited[], int st[], int &num)const{
edgeNode *p = verList[start].head;
visited[start] = true;
st[top++] = start;
// if stack is full.
if(top == m+1){
++num;
visited[start] = false;
--top;
return;
}
while( p != NULL){
if(!visited[p->end_index])
find(p->end_index, m, top, visited, st, num);
p = p->next;
}
visited[start] = false;
--top;
}
};
int main()
{
int n,m;
cin>>n>>m;
int start, M;
cin>>start>>M;
int a, b;
adjListGraph Graph(n);
for(int i=0;i<m;++i){
cin>>a>>b;
Graph.insert(a,b);
}
int res = Graph.find(start, M);
cout<<res;
return 0;
}