Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

0ms水过之~

Posted by 449338369 at 2012-12-12 19:47:27 on Problem 1094
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <functional>
using namespace std;

struct Edge
{
    int v, nex;
};
Edge edge[901];
int o;
int head[27];
int deg[27];
string result;

void addEdge(int from, int to)
{
    edge[o].v = to;
    edge[o].nex = head[from];
    head[from] = o;
    ++o;
}

enum res{ok, roop, no};

res run(int n)
{
    res ret = ok;
    result = "";
    int cnow[27];
    bool flag[27];
    memset(flag, 0, sizeof(flag));
    for(int i = 0; i < n; ++i)
    {
        cnow[i] = deg[i];
    }

    for(int i = 0; i<n;++i)
    {
//        printf("degs: ");
//        for(int i = 0; i < n; ++i)
//        {
//            printf("%c : %d ", 'A'+i, deg[i]);
//        }
//        printf("\n");
        int degZero = 0;
        int at;
        for(int j = 0; j <n; ++j)
        {
            if(flag[j] || cnow[j])
            {
                continue;
            }
            ++degZero;
            if(degZero > 1)
                continue;
            at = j;
        }
        if(degZero == 0)
        {
            ret = roop;
        }
        if(degZero > 1)
        {
            ret = no;
        }
        flag[at] = true;
        result += at + 'A';
        for(int ptr = head[at]; ptr; ptr = edge[ptr].nex)
        {
            --cnow[edge[ptr].v];
        }
    }
    return ret;
}



int main()
{
    int n, m;
    char a, b;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        if(!n && !m)
            break;
        o = 1;
        memset(head, 0, sizeof(head));
        for(int i = 0; i<27; ++i)
            deg[i] = 0;

        int at(-1);
        int end(false);
        res g;
        for(int i = 0; i < m; ++i)
        {
            scanf("  %c<%c", &a, &b);
            if(end)
                continue;
            addEdge(a - 'A', b-'A');
            deg[b-'A']++;
            g = run(n);
            if(g != no)
            {
                at = i + 1;
                end = true;
            }

        }
        if(end)
        {
            if(g == roop)
            {
                printf("Inconsistency found after %d relations.\n", at);
            }
            else
            {
                printf("Sorted sequence determined after %d relations: %s.\n", at, result.c_str());
            }
        }
        else
        {
            printf("Sorted sequence cannot be determined.\n");
        }
//        }
//        if(!ok)
//        {
//            if(at == -1)
//                printf("Sorted sequence cannot be determined.\n");
//            else
//                printf("Inconsistency found after %d relations.\n", at);
//        }
//        else
//        {
//            printf("Sorted sequence determined after %d relations: %s.\n", at, result.c_str());
//        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator