您的位置:首页 > 其它

POJ1094(模拟····拓扑)

2014-10-02 14:08 405 查看
Sorting It All Out
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status

Description

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.

解题思路:

可以用拓扑排序,也可以开二维数组模拟。题意是说锁定前n个大写字母,然后给m个关系,问能否通过这m个关系求得

最后把前n个字母排成序。

最后有三种可能:1.出现环了,不能排成序。2.所有关系都用上之后也无法排出n个字母的顺序。
3.顺利的完成排序。

对于1和3两种情况,要记录何时下结论它不能排成序、它何时能排成序。

核心部分来了。我们首先要构造一个小于关系的二维数组。如果是小于号,那么topo(a ,b),否则topo(b , a)。下面以a<b为例:

topo操作:1.如果a>b,那么明显出问题了,最终排不成序。 2.如果i < a ,那么i 一定小于b 。 3.如果j 大于b ,那么j 一定大于a.。

最后两层循环,看是否存在无法判断的情况,否则的话就一定能排成序。

如果能排成序的话,我们要输出排好序的前n个字母,在输出前我们开一个数组f,并且对它排序,要求它满足小于关系·······这里

没搞懂,我觉得如果判断排好序的话直接输出就可以,再判断一次没意义·····可是不加最后的判断就WA······我再想想反例吧······

完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

const int maxn = 30;
int small[maxn][maxn];
string str;
int n , m , res;

bool cmp(int a , int b)
{
    return small[a][b];
}

int topo(int a , int b)
{
    if(small[b][a])
        return 0;
    small[a][b] = 1;
    for(int i = 0 ; i < n ; i ++)
        if(small[i][a])
            small[i][b] = 1;
    for(int i = 0 ; i < n ; i ++)
        if(small[b][i])
            small[a][i] = 1;
    for(int i = 0 ; i < n ; i ++)
    {
        for(int j = 0 ; j < n ; j++)
        {
            if(small[i][a] && small[b][j])
                small[i][j] = 1;
        }
    }
    for(int i = 0 ; i < n ; i ++)
    {
        for(int j = i + 1 ; j < n ; j ++)
        {
            if(!(small[i][j] || small[j][i]))
                return 2;
        }
    }
    return 1;
}

void input(int i)
{
    char s[3];
    for(int j = i + 1 ; j < m ; j++)
        cin >> s;
}

void output()
{
    int f[maxn];
    for(int i = 0 ; i < n ; i ++)
        f[i] = i;
    sort(f , f + n , cmp);
   // for(int i = 0 ; i < n ; i ++)
     //   cout << f[i] << " " ;
    //cout << endl;
    for(int i = 0 ; i < n ; i ++)
        putchar(f[i] + 'A');
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif

    while(~scanf("%d%d",&n,&m))
    {
        if(n == 0 && m == 0)
            break;
        memset(small , 0 , sizeof(small));
        for(int i = 0 ; i < m ; i ++)
        {
            cin >> str;
            int a = str[0] - 'A';
            int b = str[2] - 'A';

            if(str[1] == '<')
                res = topo(a , b);
            else
                res = topo(b , a);
           // cout << res << endl;

            if(res == 0)
            {
                input(i);
                printf("Inconsistency found after %d relations.\n" , i + 1);
                break;
            }
            if(res == 1)
            {

                input(i);
                printf("Sorted sequence determined after %d relations: " , i + 1);
                output();
                printf(".\n");
                break;
            }
        }
        if(res == 2)
            printf("Sorted sequence cannot be determined.\n");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: