-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_code.cpp
128 lines (103 loc) · 2.95 KB
/
Final_code.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
#include<bits/stdc++.h>
using namespace std;
const int maxN=1e5;
vector<string> airports; // stores list of Airports
unordered_map<string,int> mp; //stores Airport's index in vector airports
vector<vector<int>> graph; // adjacency list of existing flight network
vector<bool> vis(maxN); //To check if Airport is visited or not
vector<int> disc(maxN); // stores discovery time
vector<int> low(maxN); // low[v] represents node with earliest discovery time reachable from v
vector<bool> stackMem(maxN); // represents if node-v is present in stack-st or not.
stack<int> st;
int timer=0; // current time
vector<int> repNode; //rep[u] is representative node of connected component in which node-u is present
void Tarjans(int u) // implementation of Tarjan's Algorithm using DFS
{
vis[u]=true;
disc[u]=low[u]=timer++;
stackMem[u]=true;
st.push(u);
for(auto v:graph[u])
{
if(!vis[v])
{
Tarjans(v);
low[u]=min(low[u],low[v]);
}
else if(stackMem[v])
low[u]=min(low[u],disc[v]);
}
if(low[u]==disc[u])
{
while(st.top()!=u)
{
stackMem[st.top()]=false;
repNode[st.top()]=u;
st.pop();
}
stackMem[u]=false;
repNode[u]=u;
st.pop();
}
}
int main()
{
cout<<"Total Number of Airports: "<<endl;
long n;
cin>>n;
cout<<"List of Airport: "<<endl;
airports.resize(n);
graph.resize(n);
for(int i=0;i<n;i++)
{
cin>>airports[i];
mp[airports[i]]=i;
}
cout<<"Starting Airport is Monaco Airport: MNC "<<endl;
cout<<"Total number of existing Flight routes: "<<endl;
long m;
cin>>m;
cout<<"List Flight routes: "<<endl;
//forming adjacency list of existing flight-network graph
while(m--)
{
string l,r;
cin>>l>>r;
graph[mp[l]].push_back(mp[r]);
}
repNode.resize(n);
for(int i=0;i<n;i++)
{
if(!vis[i])
{
Tarjans(i);
}
}
vector<int> indeg(n,0); //stores in-deg of representative node of each connected component in compressed graph
//in-degree of node is equal to all incoming edges
vector<vector<int>> compGraph(n); // adjacency list of compressed graph
for(int u=0;u<n;u++)
{
for(auto v:graph[u])
{
if(repNode[u]!=repNode[v])
{
compGraph[repNode[u]].push_back(repNode[v]);
indeg[repNode[v]]++;
}
}
}
//output
cout<<"New flight routes to be added: "<<endl;
long cnt=0;
for(int i=0;i<n;i++)
{
if(repNode[i]==i && indeg[i]==0)
{
cout<<"MNC "<<airports[i]<<endl;
cnt++;
}
}
cout<<"Count of new flight routes added: "<<cnt<<endl;
return 0;
}