UVA 10305 - Ordering Tasks Solution

Solution in C++:

///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh

#include<bits/stdc++.h>
using namespace std;
vector<int>g[10000];
vector<bool>v;
vector<int>r;
void dfs(int s)
{
    v[s]=1;
    for(int i=0; i<g[s].size(); i++)
    {
        int x=g[s][i];
        if(v[x]==0)
            dfs(x);
    }
    r.push_back(s);
}
int main()
{
    int nodes, edges;
    while(cin >> nodes >> edges,nodes!=0||edges!=0)
    {
        v.clear();
        v.assign(nodes+1,0);
        r.clear();
        for (int i = 0; i < edges; i++)
        {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
        }
        for(int i=1; i<=nodes; i++)
        {
            if(v[i]==0)
                dfs(i);
        }
        for(int i=nodes-1; i>=0; i--)
        {
            cout<<r[i];
            if(i)
                cout << ' ';
        }
        cout<<endl;
    }
    return 0;
}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.