UVA 10305 拓扑排序 删边法

来源:互联网 发布:软件意向购买书 编辑:程序博客网 时间:2024/06/03 12:56
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;const int maxn=105;int in[maxn],n;vector<int> edge[maxn];void init(){    for(int i=1;i<=maxn;i++)        edge[i].clear();    memset(in,0,sizeof(in));}void solve(){    queue<int> que,res;    for(int i=1;i<=n;i++)        if(in[i]==0)        {            que.push(i);        }    res=que;    while(que.size())    {        int tp=que.front();        que.pop();        for(vector<int>::iterator it=edge[tp].begin();it!=edge[tp].end();it++)        {            in[*it]--;            if(in[*it]==0)            {                que.push(*it);                res.push(*it);            }        }        in[tp]=-1;    }    while(res.size()>1)    {        printf("%d ",res.front());        res.pop();    }    printf("%d\n",res.front());    res.pop();}int main(){    int m,x,y;    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))    {        init();        while(m--&&scanf("%d%d",&x,&y))        {            edge[x].push_back(y);            in[y]++;        }        solve();    }}

0 0
原创粉丝点击