Skip to content

Commit 0baa8a2

Browse files
committed
Added more stuff
1 parent fbcab67 commit 0baa8a2

File tree

4 files changed

+136
-12
lines changed

4 files changed

+136
-12
lines changed

Advanced Graphs/Bottom.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void dfs(vector<int>* edges, int start, int* visited, stack<int> &s){
8080
}
8181

8282
unordered_set<unordered_set<int>*>* kosaraju(vector<int>* edges, vector<int>* edgest, int v){
83+
8384
//Initialize an empty set of sets for the answer
8485
unordered_set<unordered_set<int>*>* ans = new unordered_set<unordered_set<int>*>();
8586

@@ -137,6 +138,7 @@ int main( int argc , char ** argv )
137138
cin.tie(NULL) ;
138139

139140
while(1){
141+
140142
int v, e;
141143
cin>>v;
142144
if (v==0)
@@ -157,31 +159,66 @@ int main( int argc , char ** argv )
157159
}
158160

159161
unordered_set<unordered_set<int>*>* components = kosaraju(edges, edgest, v);
162+
// auto itt = components->begin();
163+
// while(itt!=components->end()){
164+
// auto itt2 = (*itt)->begin();
165+
// while(itt2!=(*itt)->end()){
166+
// cout << *itt2+1 << ' ';
167+
// itt2++;
168+
// }
169+
// itt++;
170+
// cout << '\n';
171+
// }
172+
173+
// cout << (*components).size() << '\n';
160174

161175
auto it = components->begin();
162-
while(it!=components->end()){
176+
vector<int> ans;
177+
while(it!=components->end())
178+
{
179+
int flag = 0;
163180
auto it2 = (*it)->begin();
164-
while(it2!=(*it)->end()){
165-
int i = 0;
166-
for (i = 0; i < edges[*it2].size(); ++i)
181+
while(it2!=(*it)->end())
182+
{
183+
//int i = 0;
184+
for (int i = 0; i < edges[*it2].size(); ++i)
167185
{
168-
if (it->count(edges[*it2].at(i)) == 0)
186+
187+
if ((*it)->count(edges[*it2].at(i)) == 0)
169188
{
189+
flag = 1;
170190
break;
191+
171192
}
172193
}
173-
if (i==edges[*it2].size())
194+
if (flag == 1)
174195
{
175196
break;
176197
}
177198
it2++;
178199
}
179-
while(it2!=(*it)->end()){
180-
cout << *it2 << ' ';
200+
if (flag == 0)
201+
{
202+
//vector<int> ans;
203+
it2 = (*it)->begin();
204+
while(it2!=(*it)->end())
205+
{
206+
ans.push_back(*it2 + 1);
207+
//cout << *it2 + 1<< ' ';
208+
it2++;
209+
}
210+
211+
//cout <<'\n';
212+
//break;
181213
}
182-
cout <<'\n';
214+
183215
it++;
184216
}
217+
sort(ans.begin(), ans.end());
218+
for (int i = 0; i < ans.size(); ++i)
219+
{
220+
cout<<ans.at(i)<<" ";
221+
}
185222
}
186223

187224

Advanced Graphs/Monk.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
9+
/*
10+
MONK AND THE ISLAND
11+
Monk visits the land of Islands. There are a total of N islands numbered from 1 to N. Some pairs of islands are connected to each other by Bidirectional bridges running over water.
12+
Monk hates to cross these bridges as they require a lot of efforts. He is standing at Island #1 and wants to reach the Island #N. Find the minimum the number of bridges that he shaint have to cross, if he takes the optimal route.
13+
Input:
14+
First line contains T. T testcases fointow.
15+
First line of each test case contains two space-separated integers N, M.
16+
Each of the next M lines contains two space-separated integers X and Y , denoting that there is a bridge between Island X and Island Y.
17+
Output:
18+
Print the answer to each test case in a new line.
19+
Constraints:
20+
1 ≤ T ≤ 10
21+
1 ≤ N ≤ 10000
22+
1 ≤ M ≤ 100000
23+
1 ≤ X, Y ≤ N
24+
SAMPLE INPUT
25+
2
26+
3 2
27+
1 2
28+
2 3
29+
4 4
30+
1 2
31+
2 3
32+
3 4
33+
4 2
34+
SAMPLE OUTPUT
35+
2
36+
2
37+
*/
38+
39+
using namespace std;
40+
#define pencho ios_base::sync_with_stdio(0);cin.tie(0);
41+
#define mod 1000000007
42+
int n;
43+
int dp[10001];
44+
int solve(vector<int>adj[])
45+
{
46+
queue<int>q;
47+
dp[1]=0;
48+
q.push(1);
49+
while(!q.empty())
50+
{
51+
int idx=q.front();
52+
q.pop();
53+
for(int i=0;i<adj[idx].size();i++)
54+
{
55+
if(dp[adj[idx][i]]==-1)
56+
{
57+
dp[adj[idx][i]]=dp[idx]+1;
58+
q.push(adj[idx][i]);
59+
}
60+
}
61+
}
62+
return dp[n];
63+
}
64+
int main()
65+
{
66+
pencho
67+
int t,m;
68+
cin>>t;
69+
while(t--)
70+
{
71+
cin>>n>>m;
72+
memset(dp,-1,sizeof(dp));
73+
vector<int>adj[n+1];
74+
while(m--)
75+
{
76+
int x,y;
77+
cin>>x>>y;
78+
adj[x].push_back(y);
79+
adj[y].push_back(x);
80+
}
81+
cout<<solve(adj)<<endl;
82+
}
83+
}

Advanced Graphs/Test.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
3 3
2-
1 3 2 3 3 1
3-
0
1+
1
2+
4 5
3+
1 2
4+
1 3
5+
2 3
6+
2 4
7+
3 4

Advanced Graphs/a.out

-52.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)