Critical Planet C++ Program solution
Answers
Answer:
Critical Planets
Problem Description
War between Republic and Separatist is escalating. The Separatist are on a new offensive. They have started blocking the path between the republic planets (represented by integers), so that these planets surrender due to the shortage of food and supplies. The Jedi council has taken a note of the situation and they have assigned Jedi Knight Skywalker and his Padawan Ahsoka to save the critical planets from blockade (Those planets or system of planets which can be accessed by only one path and may be lost if that path is blocked by separatist).
Skywalker is preparing with the clone army to defend the critical paths. He has assigned Ahsoka to find the critical planets. Help Ahsoka to find the critical planets(C) in ascending order. You only need to specify those planets which have only one path between them and they cannot be accessed by any other alternative path if the only path is compromised.
Constraints
M <= 10000
N <= 7000
Input
First line contains two space separated integers M and N, where M denotes the number of paths between planets and N denotes the number of planets.
Next M lines, each contains two space separated integers, representing the planet numbers that have a path between them.
Output
C lines containing one integer representing the critical planet that they need to save in ascending order of the planet number if no planet is critical then print -1
Time Limit
1
Examples
Example 1
Input
3 4
0 1
1 2
2 3
Output
0
1
2
3
Explanation
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@20fc13f9:image1.jpeg
Since all the planets are connected with one path and cannot be accessed by any alternative paths hence all the planets are critical.
Example 2
Input
7 6
0 2
0 1
1 2
2 3
4 5
3 4
3 5
Output
2
3
Explanation
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@20fc13f9:image2.jpeg
If the republic loose the path between 2 and 3 then the two system of planets will not be able to communicate with each other. Hence 2 and 3 are critical planets.
Explanation:
bhai may be its help u and mark it as braniest
Answer:
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int desc[10000];
int visited[10000];
set<int> crit;
map<int,set<int> > g;
int edges, vertices;
int currentTime = 0;
void tarjan(int u, int v) {
desc[v] = visited[v] = currentTime;
++currentTime;
for(auto adj: g[v]) {
if(visited[adj] == -1)
{
tarjan(v, adj);
if(desc[adj] < desc[v]) desc[v] = desc[adj];
if(desc[adj] == visited[adj]) {
crit.insert(v);
crit.insert(adj);
}
}
else if(adj != u) {
if(visited[adj] < desc[v]) desc[v] = visited[adj];
}
}
}
int main() {
cin >> edges;
cin >> vertices;
for(int i = 0 ; i < vertices; i++)
desc[i] = visited[i] = -1;
for(int i = 0; i < edges; i++) {
int from,to;
cin >> from;
cin >> to;
auto it = g.find(from);
if(it != g.end())
it->second.insert(to);
else {
set<int> s;
s.insert(to);
g.insert(make_pair(from, s));
}
auto it2 = g.find(to);
if(it2 != g.end())
it2->second.insert(from);
else {
set<int> s;
s.insert(from);
g.insert(make_pair(to, s));
}
}
for(int v = 0;v < vertices; v++) {
if(visited[v] == -1) {
tarjan( v, v);
}
}
vector<int> answer;
for(auto x: crit) answer.push_back(x);
sort(begin(answer), end(answer));
if(answer.size() == 0)
cout << "-1" << endl;
else {
for(auto x: answer) cout << x<< endl;
}
}