AOV网络、AOE网络(拓扑排序)

岁酱吖の 提交于 2020-01-30 00:44:22

判断是否可构成AOV网络,即是否存在拓扑序列

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n, m;
vector <int> p[110];
int inde[110];
int tp(){
    queue<int> Q;
    int i, j;
    for(i = 0; i < n; i++)
        if(!inde[i])
            Q.push(i);
    int t, cont = 0;
    while(!Q.empty()){
        t = Q.front();
        cont++;
        Q.pop();
        for(i = 0; i <p[t].size(); i++){
            inde[p[t][i]]--;
            if(!inde[p[t][i]])
                Q.push(p[t][i]);
        }

    }
    if(cont < n)
            return 0;
    return 1;
}
int main(){

    int i, j, a, b;
    while(~scanf("%d %d", &n, &m) && n){
        memset(p, 0, sizeof(p));
        memset(inde, 0, sizeof(inde));
        for(i = 1; i <= m; i++){
            scanf("%d %d", &a, &b);
            p[a].push_back(b);
            inde[b]++;
        }
        if(tp())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

输出拓扑排序序列

#include <queue>
#include <vector>
using namespace std;
int n;
int inde[110];
int topo[110];
vector <int> p[110];
void tp(){
    int i;
    queue<int> Q;
    for(i = 1; i <= n; i++){
        if(!inde[i]){
            Q.push(i);
    
        }
    }
    int t, k = 0;

    while(!Q.empty()){
        t = Q.front();
        topo[k++] = t;
        Q.pop();
        for(i = 0; i < p[t].size(); i++){
                inde[p[t][i]]--;
                if(inde[p[t][i]] == 0){
                    Q.push(p[t][i]);
                }
        }
    }

}
int main(){
    int i, ch;
    scanf("%d", &n);
    memset(inde, 0, sizeof(inde));
    for(i = 1; i <= n; i++){
        while(1){
            scanf("%d", &ch);
            if(ch == 0) break;
            else
                p[i].push_back(ch);
                inde[ch]++;
        }
    }
    tp();
    int flag = 0;
    for(i = 0; i < n; i++){
        if(flag++ == 0)
            printf("%d", topo[i]);
        else
            printf(" %d", topo[i]);
    }
    return 0;
}

判断当前已知条件是否存在矛盾或者能够构成唯一序列

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int n;
int inde[210];
int topo[210];
int cont, contt;
int vis[30];
vector <int> p[210];
int tp(){
    int i;
    int flag = 1;
    cont = 0;
    int in[30];
    for(int i = 0; i < 26; i++){
    	in[i] = inde[i];
	}
    queue<int> Q;
    for(i = 0; i < 26; i++){
        if(!in[i]&&vis[i]){
            Q.push(i);
        }
    }
    int t, k = 0;
    while(!Q.empty()){
        if(Q.size() > 1) flag = 0; 
        t = Q.front();
        topo[k++] = t;
        cont++;
        Q.pop();
        for(i = 0; i < p[t].size(); i++){
                in[p[t][i]]--;
                if(in[p[t][i]] == 0){
                    Q.push(p[t][i]);
                }
        }
    }
    if(cont < contt) //存在环 
        return -1;
    if(flag)
    	return cont;//当前已读入关系的唯一拓扑序列的顶点个数 
    return 0;//尚未存在唯一拓扑序列 

}
int main(){
    int i, ch, t, m;
    char a, b;
    int flag;
    int k;
     while(~scanf("%d%d", &n, &m) && (n || m)){
            getchar();
            flag = contt = 0;
         memset(inde, 0, sizeof(inde));
          memset(vis, 0, sizeof(vis));
            for(i = 0; i <= 26; i++)
                p[i].clear();
            for(i = 1; i <= m; i++){
                    scanf("%c<%c", &a, &b);
                    getchar();
                    p[a-'A'].push_back(b-'A');
                    inde[b-'A']++;
                    if( !vis[a-'A']) contt++;
                    if( !vis[b-'A']) contt++;
                    vis[a-'A'] = vis[b-'A'] = 1;
                    
                    if(flag == 0){
                    	int t = tp();
						if(t == -1){
							flag = -1;
							k = i;
						}
						else if(t == n){
							flag = 1;
							k = i; 
						}
							
					} 
            }
            if(flag == -1){
           	 printf("Inconsistency found after %d relations.\n", k);
                
           }
        	else if(flag == 0)
                    printf("Sorted sequence cannot be determined.\n");
          
            else{
                 printf("Sorted sequence determined after %d relations: ", k);
                for(i = 0; i < n; i++)
                printf("%c", (char)(topo[i]+'A'));
                printf(".\n");
            }
       }
    
           

    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!