【poj 1094 Sorting It All Out 】(拓扑排序判环 + 判唯一性)

题意:n个编号,m中偏序关系,形如A<B,问是否存在唯一的拓扑序列并且给出在第几组关系确定的。
分析: 拓扑排序判环 + 判唯一性
本题特别的地方在 第几组能够确定唯一的拓扑序列,所以每输入一组关系,需调用拓扑排序一次,若确定了存在唯一序列或者存在环,即可不再调用。
若直到输入完毕也不能确定,输出对应答案。
步骤:
1.存在环或者确定唯一序列,结束
2.不能确定是步骤1的情形,一直调用拓扑排序

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=1e8;
const int N=30;
const ll maxn=1LL<<60;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,m;
vector<int>g[N];
//bool g[N][N];
char ans[N];
int deg[N],num;
int in[N];
///初始化
void init()
{
    num=0;
    mem(deg,0);
    mem(in,0);
    rep(i,0,n+1) g[i].clear();
}
/// 拓扑排序
int toposort()
{
    ///puts("AA");
    queue<int>que;
    mem(ans,0);
    memc(in,deg);///多次使用拓扑排序,in[]为辅助数组
    int flag=1,cnt=0;
    rep(i,1,n+1) if(!in[i]) que.push(i);
    while(!que.empty())
    {
        if(que.size()>1) flag=0;///当前入度为0的点多于1个,说明序列不确定
        int u=que.front();
        que.pop();
        ans[cnt++]=u+'A'-1;///存储序列
        rep(i,0,g[u].size())
        {
            int v=g[u][i];
            in[v]--;
            if(!in[v]) que.push(v);
        }
    }
    //printf("flag=%d cnt=%d n=%d\n",flag,cnt,n);
    if(flag==1 && cnt==n) return 1;///每次入队只有一个点且序列全部生成,则确定
    else
    {
        rep(i,1,n+1) if(in[i]) return 2;///存在环
        return 0;///不存在环,且未生成序列
    }
}

int main()
{
    //fre;
    while(~scanf("%d %d",&n,&m)&&n+m)
    {
        init();
        int can=0,id=-1;
        rep(i,0,m)
        {
            getchar();
            char ch1,ch2;
            scanf("%c<%c",&ch1,&ch2);
            if(can) continue;
            int u=ch1-'A'+1,v=ch2-'A'+1;
            //printf("u=%d v=%d\n",u,v);
            g[u].pb(v);
            deg[v]++;
            if(!can) can=toposort();
            if(id==-1 && can) id=i+1;
            //printf("can=%d\n",can);
            //  puts("BB");
        }
        if(!can) printf("Sorted sequence cannot be determined.\n");
        else if(can==1) printf("Sorted sequence determined after %d relations: %s.\n",id,ans);
        else printf("Inconsistency found after %d relations.\n",id);

    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值