hdu 1285 拓扑排序起步

来源:互联网 发布:听力打卡的软件 编辑:程序博客网 时间:2024/06/07 07:05

拓扑排序

dfs实现拓扑排序 函数(算法竞赛入门经典)E(u,v)int c[maxn];int topo[maxn],t;bool dfs(int u){   c[u]=-1;      //开始访问该顶点   for(int v=0;v<n;v++)   {       if(G[u][v]==1)       {            if(c[v]<0) return false;   //c[v]=-1代表正在访问该定点(即递归调用dfs(u)正在帧栈中,尚未返回)            else if(!c[v] && !dfs(v))   return false;   //(c[v]==0 && dfs(v)==false即当前顶点没有后即顶点时,                                                                       //开始返回 (结束))       }   }   c[u]=1;      //访问结束   topo[--t]=u;   return true;}bool toposort(){t=n;memset(c,0,sizeof(c));for(int u=0;u<n;u++)if(!c[u]) if(!dfs())  return false;return ture;}
算法的抽象描述为:      NonSuccFirstTopSort(G){//优先输出无后继的顶点        while(G中有出度为0的顶点)do {         从G中选一出度为0的顶点v且输出v;         从G中删去v及v的所有人边         }        if(输出的顶点数目<|V(G)|)             Error("G中存在有向环,排序失败!");       }

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。
Input
输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。
Output
给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
Sample Input
4 3
1 2
2 3
4 3
Sample Output
1 2 4 3

#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,-1,-1,1,1};const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=5e4+100;const double EPS=1e-7;const int mod=1000000007;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt);    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1);    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}int _pow(LL a,LL ssss){LL ret=1;while(ssss){if(ssss&1)ret=ret*a%mod;a=a*a%mod;ssss>>=1;}return ret;}int du[maxn];int main(){    int n,m;    while(cin>>n>>m)    {        vector<int >Q[maxn];        me(du);        for(int i=1;i<=m;i++)        {            int x,y;            cin>>x>>y;            Q[x].push_back(y);            du[y]++;        }        int flag=0;        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)        {            if(du[j]==0)            {                du[j]--;                if(flag) cout<<" ";                cout<<j;                flag=1;                for(int k=0;k<Q[j].size();k++)                {                    du[Q[j][k]]--;                }                break;            }        }        //for(int i=1;i<=n;i++)         //  cout<<du[i]<<endl;        cout<<endl;    }}


0 0