【UVA 10305】 Ordering Tasks 拓撲排序模板題

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is
only possible if other tasks have already been executed.
Input
The input will consist of several instances of the problem. Each instance begins with a line containing
two integers, 1 ≤ n ≤ 100 and m. n is the number of tasks (numbered from 1 to n) and m is the
number of direct precedence relations between tasks. After this, there will be m lines with two integers
i and j, representing the fact that task i must be executed before task j.
An instance with n = m = 0 will finish the input.
Output
For each instance, print a line with n integers representing the tasks in a possible order of execution.
Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3

題意:給出n個結點,有m個對這些結點的先後關係的限制,輸出一種滿足限制條件的序列

思路(拓撲排序):

模板題,記錄一下。
先記錄每個點的入度,以所有的入度爲1的結點開始,跑一個bfs,每次將當前點所連接的點入度-1並壓進隊列入度爲0的連接點,如此往復將所有點遍歷一遍
對於這道題,注意一下輸入輸出格式就好了

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define maxn 1000+500
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxm = 100000+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
vector< vector<ll> > D(maxn);
ll n;
ll m;
ll vis[maxn];
ll in[maxn];vector<ll> ans;

void init()
{
    mem(vis,0);mem(in,0);
    rep(i,1,n) D[i].clear();
    ans.clear();
}

void Topsort()
{
    queue<ll> q;
    rep(i,1,n) if(in[i]==0) q.push(i), vis[i] = 1;
    while(!q.empty())
    {
        ll cur = q.front(); q.pop();
        if(in[cur] == 0) ans.pb(cur);
        if(D[cur].size()==0) continue;
        rep(i,0,D[cur].size()-1)
        {
            ll v = D[cur][i];    in[v] --;
            if(!vis[v]&&in[v]==0)
            {
                q.push(v);
                vis[v] = 1;
            }
        }
    }
}

int main()
{
    FAST;
    while(cin>>n>>m&&(n||m))
    {
        init();
        rep(i,1,m)
        {
            ll x,y;
            cin>>x>>y;
            D[x].pb(y);
            in[y] ++;
        }
        Topsort();
        for(int i=0;i<ans.size()-1;i++) cout<<ans[i]<<' ';
        cout<<ans[ans.size()-1]<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章