【POJ 1094】Sorting It All Out (邻接表)

 

n 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.

 

 

 

有如下三种情况

如果可以确定唯一的排序顺序,输出顺序

有回环

顺序不唯一

这道题稍微有点坑人,要输出经过多少步骤可以得出结论,所以每次输入之后都要进行拓扑排序进行判断,还要对输入的关系进行标记,重复输入时不作处理

AC代码:

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<vector>
#include<list>
using namespace std;
const int MX = 30;
int n, m, tot, s, ind, IN[MX], head[MX], rec[MX];
int S[MX], flag, vis[MX];
char ch[MX];
struct Edge{
    char v;
    int nxt;
}e[10010];
void add(int u, int v){
    e[tot].v = v;
    e[tot].nxt = head[u];
    head[u] = tot++;
    IN[v]++;
}
void topo_sort(){
    queue<int> q;
    memset(S, 0, sizeof(S));
    for(int i = 0; i < n; i++)
        rec[i] = IN[i];
    for(int i = 0; i < n; i++)
        if(vis[i] && rec[i] == 0)
            q.push(i);
    ind = 0;
    int temp = 0;
    while(!q.empty())
    {
        if(q.size() > 1)
            temp = 1;
        int v = q.front();
        S[ind++] = v;
        q.pop();
        for(int i = head[v]; ~i; i = e[i].nxt)
        {
            int u = e[i].v;
            rec[u]--;
            if(rec[u] == 0)
            {
                q.push(u);
            }
        }
    }
    if(!temp && ind == n)
        flag = 2;
    if(ind == s && s < n)
        flag = 0;
    if(ind < s)
        flag = 1;
}
int main()
{
    char c[5];
    int x, y;
    while((~scanf("%d%d", &n, &m)) && (n || m))
    {
        memset(IN, 0, sizeof(IN));
        memset(head, -1, sizeof(head));
        memset(vis, 0, sizeof(vis));
        s = 0;
        tot = 0;
        flag = 0;
        for(int i = 1; i <= m; i++)
        {
            scanf("%s", c);
            if(flag)
                continue;
            x = c[0] - 'A';
            y = c[2] - 'A';
            if(!vis[x])
            {
                vis[x] = 1;
                s++;
            }
            if(!vis[y])
            {
                vis[y] = 1;
                s++;
            }
            add(x, y);
            topo_sort();
            if(flag == 2)
            {
                cout<<"Sorted sequence determined after "<<i<<" relations: ";
                for(int i = 0; i < n; i++)
                    cout<<char(S[i] + 'A');
                cout<<"."<<endl;
            }
            if(flag == 1)
            {
                cout<<"Inconsistency found after "<<i<<" relations."<<endl;
            }
        }
        if(flag == 0)
            cout<<"Sorted sequence cannot be determined."<<endl;
    }
    return 0;
}

 

 

 

这有一份拓扑模板,但是并不适用于本题目,不过还是会有一定的启发作用。邻接表的实现可以减少空间的浪费

 

 

#include<bits/stdc++.h>
using namespace std;
const int MX = 1e5 +5;
struct Edge{
    int v,nxt;
}E[MX*2];
int head[MX],tot,IN[MX];
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
    memset(IN,0,sizeof(IN));
}
void add(int u,int v){
    E[tot].v=v;
    E[tot].nxt=head[u];
    head[u]=tot++;
    IN[v]++;
}
int vec[MX],sz;
bool top_sort(int n){
    sz=0;
    queue<int>q;
    for(int i=1;i<=n;i++) if(IN[i]<=1) q.push(i);
    while(!q.empty()){
        int u=q.front();q.pop();
        vec[++sz]=u;
        for(int i=head[u];~i;i=E[i].nxt){
            int v=E[i].v;
            IN[v]--;
            if(IN[v]<=1) q.push(v);
        }
    }
    if(sz!=n) return 0;
    return 1;
}
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i=1;i<=m;i++) {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);add(v,u);
        }
        if(top_sort(n)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值