-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinvert_graph.cpp
49 lines (43 loc) · 923 Bytes
/
invert_graph.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
#include<bits/stdc++.h>
using namespace std;
void adjListGraph(vector<int> adj[], int s, int d){
// directed graph
adj[s].push_back(d);
}
void invertGraph(vector<int>iadj[], vector<int>adj[], int v){
for(int i=0;i<v;i++){
for(auto x: adj[i]){
iadj[x].push_back(i);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int v,e;
cin>>v>>e;
vector<int> adj[v];
while(e--){
int s,d;
cin>>s>>d;
adjListGraph(adj,s,d);
}
int s=0; // source is node 0
vector<int>iadj[v];
invertGraph(iadj, adj, v);
for(int i=0;i<v;i++){
cout<<i<<" ->";
for(auto x: adj[i]){
cout<<x<<" ";
}
cout<<endl;
}
cout<<endl;
for(int i=0;i<v;i++){
cout<<i<<" -> ";
for(auto x: iadj[i]){
cout<<x<<" ";
}
cout<<endl;
}
}