-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
223 lines (193 loc) · 5.1 KB
/
graph.cpp
File metadata and controls
223 lines (193 loc) · 5.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<bits/stdc++.h>
/*-------------------------------------------------------- */
using namespace std;
/*-------------------------------------------------------- */
#define rep(i,val,n) for(ll i=val;i<n;i++)
#define per(j,val,n) for(ll j=val;j>=n;j--)
#define pb push_back
#define pi 3.14159265
#define mp make_pair
#define MODULO 1000000007
#define INF 1000000000000000
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL);
/*-------------------------------------------------------- */
typedef long double ld;
typedef long long ll;
typedef vector<bool> boolean;
typedef vector<ll> vec;
/*-------------------------------------------------------- */
ll gcd(ll a, ll b)
{
if(b == 0)
{
return a;
}
return gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return ((a*b)/gcd(a,b));
}
/*---------------------------------------------------------*/
class graph
{
public:
ll n,r;
set<string> nodes;
vector < vector< pair<ll,ll> > > graphcon; //assuming only one edge between 2 nodes
vector< string > names,relations; //because having more does not make sense...
graph(ll conn)
{
cin>>n;
for(ll i=0;i<n;i++)
{
string s;
cin>>s;
nodes.insert(s);
names.pb(s);
}
cin>>r;
ll count=0;
vector < vector< pair<ll,ll> > > graphconx(n);
for(ll i=0;i<r;i++)
{
ll arr[n][n];
string s1;
cin>>s1;
relations.pb(s1);
for(ll j=0;j<n;j++)
{
for(ll k=0;k<n;k++)
{
cin>>arr[j][k];
if(arr[j][k]==1)
{
graphconx[j].pb({k,count});
}
}
}
graphcon=graphconx;
count++;
}
}
void display_graph()
{
for(ll i=0;i<n;i++)
{
cout<<names[i]<<endl;
for(ll j=0;j<graphcon[i].size();j++)
{
cout<<relations[graphcon[i][j].second]<<" "<<names[graphcon[i][j].first]<<endl;
}
cout<<endl;
}
}
void find_person(string s2)
{
if(nodes.find(s2)==nodes.end())
{
cout<<"NOT FOUND"<<endl;
}
else
{
cout<<"FOUND"<<endl;
}
}
void find_path(string s1,string s2)
{
vector< pair<ll,ll> > parent(n);
ll flag=0;
ll source,dest;
for(ll i=0;i<2;i++)
{
for(ll j=0;j<n;j++)
{
if(names[j]==s1)
{
source=j;
}
if(names[j]==s2)
{
dest=j;
}
}
if(i==1)
{
swap(source,dest);
}
ll visited[n];
for(ll j=0;j<n;j++)
{
visited[j]=0;
}
queue<ll> q;
q.push(source);
visited[source]=1;
while(q.size()>0)
{
ll cvert=q.front();
q.pop();
for(ll j=0;j<graphcon[cvert].size();j++)
{
if(visited[graphcon[cvert][j].first]==0)
{
q.push(graphcon[cvert][j].first);
visited[graphcon[cvert][j].first]=1;
parent[graphcon[cvert][j].first].first=cvert;
parent[graphcon[cvert][j].first].second=graphcon[cvert][j].second;
}
}
}
if(visited[dest]==1)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"NO PATH"<<endl;
}
else
{
vector< pair< ll,ll > > vs;
ll temp=dest;
while(temp!=source)
{
vs.pb(parent[temp]);
temp=parent[temp].first;
}
for(ll j=vs.size()-1;j>=0;j--)
{
cout<<names[vs[j].first]<<" "<<relations[vs[j].second]<<" ";
}
cout<<names[dest]<<endl;
}
}
friend ostream&operator<<(ostream&stream,graph obj);
};
ostream&operator<<(ostream&stream,graph obj)
{
obj.display_graph();
return stream;
}
/*---------------------------------------------------------------------*/
int main()
{
graph g(1); //creates a object of user defined type graph....
cout<<"the graph is"<<endl;
cout<<g; // overloaded << operator (prints the graph)
cout<<endl;
// cout<<"input name of the person you want to be found"<<endl;
string k;
cin>>k;
g.find_person(k);
cout<<endl;
// cout<<"input 2 names to you want to find a path between"<<endl;
string name1,name2;
cin>>name1>>name2;
cout<<endl;
g.find_path(name1,name2);
return 0;
}
/*--------------------------------------------------------------------*/