拓扑排序 模板+图的两种表示方式

https://www.cnblogs.com/1242118789lr/p/6740930.html

可以用邻接表也可以用邻接矩阵

当数据量级很大的时候我们选择邻接表,数据量小的时候可以选择邻接表也可以选择邻接矩阵

如UVA10305 

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
 第一种:,邻接表(vector存图)

#include<iostream>
#include<queue>
#include<cstring> 
#include<vector>
using namespace std;
const int N= 1e4+7;
vector<int>ve[N];
int in[N];
int main(){
    int n,m;
    while(cin>>n>>m){
    if(n==0&&m==0) break;
    queue<int>que; 
    memset(in,0,sizeof(in));
    int x,y;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        in[y]++;
        ve[x].push_back(y);//保存的是x可以到达y
    }
    for(int i=1;i<=n;i++){
        if(in[i]==0){ 
            que.push(i);
        }
    }
    int n1=n;
    while(que.size()){
        int y=que.front();
        que.pop();
        n1--;
        if(n1==0)
        printf("%d\n",y);
        else printf("%d ",y);
        for(int i=0;i<ve[y].size();i++){
            in[ve[y][i]]--;
            if(in[ve[y][i]]==0){
                que.push(ve[y][i]);
            }
        } 
    }
    for(int i=1;i<=n;i++){
        ve[i].clear();
    }  
}
    
    return 0;
} 

第二种用邻接矩阵

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=200;
int arr[N][N];
int in[N];
int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0) break; 
        queue<int >que;
        memset(arr,0,sizeof(arr));
        memset(in,0,sizeof(in));
        int x,y;
        for(int i=1;i<=m;i++){
            cin>>x>>y;
            arr[x][y]=1;//可以到达的话就是1
            in[y]++;
        }
        for(int i=1;i<=n;i++){
            if(in[i]==0){
                que.push(i);    
            }    
        }
        int n1=n;
        while(que.size()){
            int xx=que.front();
            que.pop();
            n1--;
            if(n1==0){
                printf("%d\n",xx);
            }
            else { 
                printf("%d ",xx);
            }
            for(int i=1;i<=n;i++){
                if(arr[xx][i]){
                    in[i]--;
                    if(in[i]==0){
                        que.push(i);
                    } 
                } 
            } 
        }
        
        
    }
    
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Accepting/p/11314685.html
今日推荐