最近觉得拓扑排序实在是一种优美的算法,不过我主要还是习惯bfs来拓扑排序,与很多人的dfs的习惯不符,但是bfs也很美,不是吗?

  众所周知,只有有向无环图(DAG)才可以拓扑排序,其本质就是将图上的点按顺序排列,如果有环,那自然就不会有顺序了。想要进行拓扑排序,需要存储图的入度,如果这个点的入度为0,那么它的优先级最高,排在前面,当然,对于图而言,入度为0的点可以有多个,那么,谁在前面都无所谓。

  今天讲基于bfs的拓扑排序,它有两种思路,即1.无前驱的顶点优先,2.无后继的顶点优先。这两种本质相同,只要讲第一种即可。

  无前驱的顶点优先的BFS拓扑排序,其方法是1.先找到所以入度为0的点,放入队列,作为起点,这些点无所谓前后关系,如果没有入度为0的点,说明不是DAG,不存在拓扑序。2.弹出队首元素,其后继节点入度减1,将入度为0的后继放入队列。3.继续上述操作,直到队列清空。

  很明显,总复杂度为O(V+E),并不高,可以解决较大数据规模的问题。

例题1:hdu 3342

Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17150    Accepted Submission(s): 7960

Problem Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?
We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.
Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
Sample Output
YES
NO
  很明显就是拓扑排序的模板题,只要判断能否拓扑排序即可,跑一遍BFS就出了,代码如下。
#include <bits/stdc++.h>
const int maxn=105;
using namespace std;
int ru[maxn];
using ll = long long;
vector<int>g[maxn];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        memset(ru,0,sizeof(ru));
        for(int i=0;i<n;i++)g[i].clear();
        while(m--){
            int x,y;
            cin>>x>>y;
            g[x].push_back(y);
            ru[y]++;
        }
        queue<int> q;
        for(int i=0;i<n;i++){
            if(!ru[i])q.push(i);
        }
        while(!q.empty()){
            int p=q.front();q.pop();
            for(int i=0;i<g[p].size();i++){
                int nex=g[p][i];
                ru[nex]--;
                if(!ru[nex])q.push(nex);
            }
        }
        int flag=0;
        for(int i=0;i<n;i++){
            if(ru[i]){
                flag=1;
                break;
            }
        }
        if(flag)cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}

例题2:hdu 2647

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19444    Accepted Submission(s): 6261

Problem Description
Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
Input
One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1
  用一个pri[]数组保存每个人的钱,反向建边,搜索的时候取max,因为可能之前搜索到过,然后求一下和,再检验一下是否符合拓扑序,不符改为-1,输出答案即可,代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=10005;
int ru[maxn];
int pri[maxn];
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        vector<int> g[maxn];
        memset(ru,0,sizeof(ru));
        fill(pri+1,pri+n+1,888);
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            g[y].push_back(x);
            ru[x]++;
        }
        queue<int> q;
        for(int i=1;i<=n;i++){
            if(!ru[i])q.push(i);
        }
        ll ans=0;
        while(!q.empty()){
            int p=q.front();q.pop();
            for(int i=0;i<(int)g[p].size();i++){
                int nex=g[p][i];
                ru[nex]--;
                pri[nex]=max(pri[nex],pri[p]+1);
                if(!ru[nex]){
                    q.push(nex);
                }
            }
        }
        for(int i=1;i<=n;i++)ans+=pri[i];
        for(int i=1;i<=n;i++){
            if(ru[i]){
                ans=-1;
                break;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

例题3:hdu 1285

确定比赛名次

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 59027    Accepted Submission(s): 21448

Problem Description
有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>
using namespace std;
const int maxn=505;
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        vector<int> g[maxn];
        int ru[505];
        int a[505];
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            g[x].push_back(y);
            ru[y]++;
        }
        priority_queue<int,vector<int>,greater<int> >q;
        for(int i=1;i<=n;i++){
            if(!ru[i]){
                q.push(i);
            }
        }
        int cnt=0;
        while(!q.empty()){
            int p=();
            q.pop();
            a[++cnt]=p;
            for(int i=0;i<(int)g[p].size();i++){
                ru[g[p][i]]--;
                if(!ru[g[p][i]])q.push(g[p][i]);
            }
        }
        for(int i=1;i<n;i++)printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }
    return 0;
}

摸了一天????,写一篇博客,假装做了一点事。