Topological sorting -POJ1964

                                                         Topological sorting

1. What is the topological sort?

For a directed acyclic graph (Directed Acyclic Graph referred to as the DAG) G be topologically sorted, all the vertices of G is arranged in a linear sequence, such that any pair of vertices FIG u and v, if the edge (u, v) ∈ E (G), the linear sequence u appears before v. Typically, such a sequence is called a linear sequence satisfy topological order (Topological Order), acronym topology sequence. Briefly, to give a total order on the set of a partial order on a set, this operation is called Topological Sort.

That is a vertex sorted pointed at him in front of the apex side does not exist.

2. topological sort of application?

Topological sorting is often used to determine the concentration of a dependency order of things happen. For example, in the daily work, will project may be split into A, B, C, D to accomplish four sub-portions, but A and B depend on D, C depends on D. To calculate the sequence of items, and can be topologically sorted this relationship set, drawn in a linear sequence, that is, the top surface of the tasks required to complete the task.

3. How topological sorting?

For a given graph, first find the 0 ° point, then this point is sorted first vertex, then he points of the vertices minus one, and then look for the next up-degree vertex 0 find all vertices, but have topological sort can not be applied in the drawing ring, since there is a 0-degree FIG ring vertex does not exist.

int topsort(int x)
{
    priority_queue<int ,vector<int> ,greater<int > >q;
    int flag=0,k;
    int zeroflag=0;
    int num=0;
    memcpy(temp,in,sizeof(in));
    for(int i=0;i<n;i++)
    {
        if(in[i]==0) q.push(i);
    }
    while(!q.empty())
    {
        if(q.size()>1) zeroflag=1;
        k=q.top();
        q.pop();
        a ++ ;
        ans[num]=k+'A';
     for(int i =  0 ; i < x; i++ )
            if(map[k][i]==1 && --temp[i] == 0)
                q.push(i);     
    }
    //cout<<num<<" "<<x<<endl;
    if(num!= x) return 1;
    if(zeroflag==1) return 0;
    return -1;
}

======== separator ======================================== ================================================== ===

                                                                  POJ 1964 SORTING IT ALL

 

Topic links: https://vjudge.net/problem/POJ-1094

Subject to the effect: For n capital letters, their relationship to you, ask the judge how many relationships go through, you can determine whether they are able to see the relationship between the sorts, or they have a conflict

This question is the relationship of each entry should be carried out to judge us, like columns in addition to outside, for example to give you the following three relationships,

A <B, B <C, C <A is formed at this time between the apparent A, B, C of a ring, but after the first two inputs of the relationship, we can determine the relationship between the three ABC, so You can sort the answer should be output instead

Present in the ring.

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 50
int n,m;
int in[maxn],temp[maxn];
bool map[maxn][maxn];
char ans[maxn];
int topsort(int x)
{
    priority_queue<int ,vector<int> ,greater<int > >q;
    int flag=0,k;
    int zeroflag=0;
    int num=0;
    memcpy(temp,in,sizeof(in));
    for(int i=0;i<n;i++)
    {
        if(in[i]==0) q.push(i);
    }
    while(!q.empty())
    {
        if(q.size()>1) zeroflag=1;
        k=q.top();
        q.pop();
        a ++ ;
        ans[num]=k+'A';
     for(int i =  0 ; i < x; i++ )
            if(map[k][i]==1 && --temp[i] == 0)
                q.push(i);     
    }
    //cout<<num<<" "<<x<<endl;
    if(num!= x) return 1;
    if(zeroflag==1) return 0;
    return -1;
}
int main ()
{
    int step,circleflag,orderflag;
    char s[5];
    while(cin>>n>>m)
    {
     circleflag=0;orderflag=0;
        if(n==0&&m==0) return 0;
        memset(map,0,sizeof(map));
        memset(in,0,sizeof(in));
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s);
            if(circleflag==0&&orderflag==0)
            {
                if(map[s[2]-'A'][s[0]-'A']==1)
                {
                    circleflag=1;
                    printf("Inconsistency found after %d relations.\n", i);
                    continue;
                }
                if(map[s[0]-'A'][s[2]-'A']==0)
                {
                    map[s[0]-'A'][s[2]-'A']=1;
                    in[s[2]-'A']++;
                }
                int k=topsort(n);
                if(k==1)
                {
                    circleflag=1;
                    printf("Inconsistency found after %d relations.\n", i);
                }
                else if(k==-1)
                {
                    orderflag=1;
                    step=i;
                }
            }
        }
        if(circleflag==0&&orderflag==0)
     printf("Sorted sequence cannot be determined.\n");
        else if(orderflag==1)
        {
            printf("Sorted sequence determined after %d relations: ", step);
            for(int i=1;i<=n;i++)
            {
                printf("%c",ans[i]);
                
            }
            printf(".\n");
        }        
    }
    return 0;
}
AC CODE

You can also look for the meaning of the title of this blog: https://blog.csdn.net/lyy289065406/article/details/6645999

^-^

Guess you like

Origin www.cnblogs.com/tombraider-shadow/p/11163489.html