【POJ 1094】Sorting It All Out 【topo排序 的三种情况的特判】

学习了,
第一道 拓扑
求拓扑的 算法思路
拓扑排序。拓扑排序:若G包含有向边(U,V),则在序列中U出现在V之前,即该序列使得图中所有有向边均从左指向右。如果图是有回路的,就不存在这样的序列。首先选择一个无前驱的顶点(即入度为0的顶点,图中至少应该有一个这样的顶点,否则肯定存在回路),然后从图中移去该顶点以及由其发出的所有有向边,如果图中还存在无前驱的顶点,则重复上述操作,直到操作无法进行。如果图不为空,说明图中存在回路,无法进行拓扑排序;否则移出的顶点的顺序就是对该图的一个拓扑排序。

此题 的还有一个 坑点就是,对于每种情况的输出是有优先级的,只要成环一定输出,所以当当前的 序列为无序的时候,也不能输出,因为之后可能导致成环 ,。。逻辑
代码
Sorting It All Out
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
Input
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character “<” and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
Output
For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy…y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy…y is the sorted, ascending sequence.
Sample Input

4 6
A < B
A < C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined..

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M  50
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
int n,m; 
int mp[M][M];  //  建图 
int in[M];  //  入度情况 
int mark[M]; //  每个条件之后,的入度情况 
int topo[M];  //存储着topo序列 
void init()  // 初始化  
{
    CLR(in,0);
    CLR(mp,0);
}
void trans()   //  赋值给 标记数组 
{
    int i,j;
    for(i=1;i<=n;i++)
    mark[i]=in[i];
}
int toposort()
{
    int num;   // 记录 每一次 找的时候有几个 入度为0 的个数 
    int t=0;   // 标记 topo 的数组 
    int i,j;
    int flag=1; // 记录 是无序还是 有序 
    int next;  // 记录下一个将要并入 topo的序号 
    trans();  // 获取一次当前 的入度情况 
    for(i=1;i<=n;i++)
    {
        num=0;
        for(j=1;j<=n;j++)
        {
            if(!mark[j]) 
            {
                num++;
                next=j;
            }
        }
        if(!num) return 0;  // 成环了  
        if(num>1)  flag=-1; // 这里可以判定 一定是无序的,但是 之后还可能是成环的情况 所以这里只能标记 
        topo[t++]=next;  //  并入数组 
        mark[next]=-1;  //  将next 这个顶点删除掉 
        for(j=1;j<=n;j++)
        {
            if(mp[next][j])  //以next为起点的有向边都删除掉 
            mark[j]--;   
         } 
     } 
     return flag;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        init();
        char c[5];int a,b;
        int exist;int flag=0;
        for(  i=0;i<m;i++)
        {   
        scanf("%s",c);
        if(flag)  continue; //虽然已经 得出结果,但是 输入还没有完,要把输入输完 
        a=c[0]-'A'+1;
        b=c[2]-'A'+1;
        mp[a][b]=1;
        in[b]++;
        exist=toposort();
        if(!exist) 
        printf("Inconsistency found after %d relations.\n",i+1),flag=1;
         else  if(exist==1)  // 
         {
            printf("Sorted sequence determined after %d relations: ",i+1);
            for(  j=0;j<n;j++)
            printf("%c",topo[j]+'A'-1);
            printf(".\n");
            flag=1; 
         } 
        }
        if(!flag)   // 既没有 成环,也没有排序成功 
        printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

2018.2.9 又做到一次 , 记录一下。
代码

#include<cstring>
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#define LL long long

const int N = 30;
const int M = 1e6;
const int mod = 7653;
const int inf = 0x3f3f3f3f;

vector<int>ve[N];int in[N],inn[N];
void init(int n){
    for(int i=0;i<=n;i++) {
        ve[i].clear();
        inn[i]=0;
    }
}
string ans;
int topo(int n){
    for(int i=0;i<n;i++) in[i]=inn[i];
    queue<int>Q; int cnt; ans="";
    bool flag=1; cnt=0;
    for(int i=0;i<n;i++){
        if(!in[i]) {
            Q.push(i);
            cnt++;
        }
    }
    if(cnt>1) flag=0;
    while(!Q.empty()){
        int now=Q.front(); Q.pop(); ans+='A'+now;  cnt=0;
        for(int i=0;i<ve[now].size();i++){
            int v=ve[now][i];
            if(--in[v]==0){
                Q.push(v);
                cnt++;
            }
        }
        if(cnt>1) flag=0;
    }
    if(ans.size()!=n) return -1;  // 成环了
    else if(flag) return 1; // 有完全确定的序列
    else return 0; // 序列有多种情况
}
char s[10];
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==m&&n==0) break;
        int flag=0; init(n);
        for(int i=1;i<=m;i++){
            scanf("%s",s);
            int a=s[0]-'A'; int b=s[2]-'A';
            if(s[1]=='<') {ve[a].push_back(b);inn[b]++;}
            else  { ve[b].push_back(a);inn[a]++; }
            if(!flag){
                int z=topo(n);
                if(z==1) {
                    flag=1;
                    printf("Sorted sequence determined after %d relations: ",i);
                    cout<<ans<<".\n";
                }else if(z==-1) {
                    flag=1;
                    printf("Inconsistency found after %d relations.\n",i);
                } else if(z==0)  continue;
            }
        }
        if(!flag) puts("Sorted sequence cannot be determined.");
    }
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值