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

大牛们帮我看一下啊 orz..

Posted by zamanewby at 2006-07-18 14:28:17 on Problem 1094
前面相关的讨论我都看了, 所有提到的数据我也都测试, 没有问题。 可是一提交还是WA, 大家帮忙看一下是什么问题啊

谢谢啦

P.S. 我是初学者

#include<cstdio>
#include<string>
using namespace std;

int n, inpl, dg[30], nv[30];
char str[10];
bool done, letter[30], adj[30][30];

bool TopologicalSort(){
    int InDegree[n+1];
    memset(InDegree, 0, sizeof(InDegree));
    for(int j=1; j<=n; j++)
        for(int i=1; i<=n; i++)
            if(adj[i][j]){
                InDegree[j]++;
                dg[j]++;
            }
    int stack[n+1], v[n+1];
    memset(stack, 0, sizeof(stack));
    memset(v, 0, sizeof(v));
    int top = 0, k = 0;
    for(int i=1; i<=n; i++)
        if(!InDegree[i]&&letter[i]) stack[++top] = i;
    while(top){
        int tmp;
        tmp = stack[top--];
        v[++k] = tmp;
        for(int j=1; j<=n; j++)
            if(adj[tmp][j]&&letter[j]&&InDegree[j]>0){
                InDegree[j]--;
                if(!InDegree[j]) stack[++top] = j;
            }
    }
    int count = 0;
    for(int i=1; i<=n; i++)
        if(letter[i]) count++;
    return (k==count);
}

bool bfs(int x){
    memset(nv, 0, sizeof(nv));
    int queue[700], head = 0, rear = 0;
    bool m[n+1];
    memset(queue, 0, sizeof(queue));
    memset(m, 0, sizeof(m));
    nv[x] = 1;
    for(int j=1; j<=n; j++)
        if(adj[x][j]){
            nv[j] = nv[x]+1;
            queue[++rear] = j;
            m[j] = true;
        }
    while(head<rear){
        int current = queue[++head];
        m[head] = false;
        for(int j=1; j<=n; j++)
            if(adj[current][j]){
                nv[j] = nv[current] + 1;
                if(!m[j]){
                    queue[++rear] = j;
                    m[j] = true;
                }
            }
    }
    int prev = 0;
    for(int i=1; i<=n; i++)
        if(nv[i]==prev) return false;
        else prev = nv[i];
//    sort(nv+1, nv+n+1);
    return true;
}

int main(){
//    freopen("test.txt", "r", stdin);
    while(1){
        scanf("%d%d", &n, &inpl);
        if(!n&&!inpl) break;
        done = false;
        memset(adj, 0, sizeof(adj));
        memset(dg, 0, sizeof(dg));
        memset(letter, 0, sizeof(letter));
        for(int k=1; k<=inpl; k++){
            scanf("%s", str);
            if(!done){
                adj[str[0]-'A'+1][str[2]-'A'+1] = 1;
                letter[str[0]-'A'+1] = letter[str[2]-'A'+1] = true;
                if(TopologicalSort()){
                    bool flag = true;
                    for(int i=1; i<=n; i++)
                        if(!letter[i]){
                            flag = false;
                            break;
                        }
                    if(flag){
                        int count = 0;
                        int tmp = 0;
                        for(int i=1; i<=n; i++)
                            if(!dg[i]){
                                tmp = i;
                                count++;
                            }
                        if(count>1){
                            if(k==inpl)
                                puts("Sorted sequence cannot be determined.");
                        }
                        else{
                            if(bfs(tmp)){
                                printf("Sorted sequence determined after %d relations: ", k);
                                for(int i=1; i<=n; i++)
                                    for(int j=1; j<=n; j++)
                                        if(nv[j]==i) printf("%c", j+'A'-1);
                                puts(".");
                                done = true;
                            }
                            else{
                                if(k==inpl)
                                    puts("Sorted sequence cannot be determined.");
                            }
                        }
                    }
                    else
                        if(k==inpl)
                            puts("Sorted sequence cannot be determined.");
                }
                else{
                        printf("Inconsistency found after %d relations.\n", k);
                        done = true;
                }
            }//if
        }//for
    }//while
//    while(1);
}

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