পোস্টগুলি

জুলাই, ২০১৭ থেকে পোস্টগুলি দেখানো হচ্ছে

Topological Sorting_2

#include<bits/stdc++.h> using namespace std; long long visited[100]; vector <int> grp[200]; map<int,int>vis; stack<int>st; int n; int topologicalSortUtill(int v) {     vis[v] = 1;     int l=grp[v].size();     int u;     for(int i=0;i<l;i++)     {         u=grp[v][i];         if (!vis[u])            topologicalSortUtill(u);     }     st.push(v); } int  topologicalSort()  {      for(int i=0;i<n;i++)         vis[i]=0;      for(int i=0;i<n;i++)         if(vis[i]==0)         topologicalSortUtill(i);         while(st.empty()==0)         {             cout<<st.top()<<" ";             st.pop();         }  } int main() {     int m,i,j,a,b;     cin>>n>>m;     for(i=1; i<=m; i++)     {         cin>>a>>b;         grp[a].push_back(b);     }     //for(i=0;i<n;i++)    topologicalSort();     return 0; } /* 6 6 5 2 5 0 4 0 4 1 2 3 3 1 */

Topological Sorting

#include<bits/stdc++.h> using namespace std; long long visited[100]; vector <int> grp[200]; map<int,int>vis; int n; int topologicalSortUtill(int v, stack<int> &St) {     vis[v] = 1;     int l=grp[v].size();     int u;     for(int i=0;i<l;i++)     {         u=grp[v][i];         if (!vis[u])            topologicalSortUtill(u, St);     }     St.push(v); } int  topologicalSort()  {      stack<int>st;      for(int i=0;i<n;i++)         vis[i]=0;      for(int i=0;i<n;i++)         if(vis[i]==0)         topologicalSortUtill(i,st);         while(st.empty()==0)         {             cout<<st.top()<<" ";             st.pop();         }  } int main() {     int m,i,j,a,b;     cin>>n>>m;     for(i=1; i<=m; i++)     {         cin>>a>>b;         grp[a].push_back(b);     }     //for(i=0;i<n;i++)    topologicalSort();     return 0; } /* 6 6 5 2 5 0 4 0 4 1

DFS-2

#include<bits/stdc++.h> using namespace std; long long visited[100]; vector <int> grp[200]; int n;  map<int,int>vis; int dfs(int s) { stack<int>st;     st.push(s);     //for(int i=0; i<n; i++)         //cout<<i<<" b "<<vis[i]<<endl;     while(!st.empty())     {         s=st.top();         //cout<<s<<endl;         st.pop();         if(vis[s]==0)         {             cout<<s<<" ";             vis[s]=1;         }         int l=grp[s].size();         //int l=9;         for(int i=0; i<l; i++)         {             //cout<<"grp[s][i]="<<grp[s][i]<<endl;             if(vis[grp[s][i]]==0)                 st.push(grp[s][i]);         }     } } int main() {     int m,i,j,a,b;     cin>>n>>m;     for(i=1; i<=m; i++)     {         cin>>a>>b;         grp[a].push_back(b);     }     for(i=0;i<n;i++)      

DFS

#include<bits/stdc++.h> using namespace std; long long visited[100]; vector <int> grp[200]; int n=5; map<int,int>vis; int dfs(int s) {     stack<int>st;     st.push(s);     while(!st.empty())     {         s=st.top();         st.pop();         if(vis[s]==0)         {             cout<<s<<" ";             vis[s]=true;         }     int l=grp[s].size();         //int l=9;         for(int i=0;i!=l;i++)             if(!vis[grp[s][i]])             st.push(grp[s][i]);     } } int main() {    grp[1].push_back(0);    grp[0].push_back(3);    grp[0].push_back(2);    grp[2].push_back(1);    grp[1].push_back(4);    for(int i=0;i<=5;i++)     vis[i]=0;    dfs(0);    return 0; }