nyoj349 poj1094 sorting It all out (topological sort)

Source: Internet
Author: User

nyoj349 Topic Links
poj1094 Topic Links
The two questions are the same, but on the POJ A is really the case, NY on the data is a bit weak.

The main problem is input n,m. There is a total of n letters (starting from a), and the M-line statement "X﹤y" for each statement describes the partial-order relationship between X, Y. Let you decide if you can get a unique ascending sequence of these relationships, and if you can output this sequence and indicate how many previous statements are made, if there is a contradiction between the n letters, output the corresponding statement and indicate that the statement begins to appear contradictory. If there is no unique sequence and there is no contradiction, the output wants information.
Analysis:
The algorithm topology is used to sort the u that corresponds to each edge (u,v) of the graph G to the front of v. It is not difficult to find out if there is another ring in the diagram, there is no topological ordering. Topological ordering answers can be multiple (for example: a﹤b; C﹤b; D﹤c; The sort can be ADCB or DACB), but the order is required to be unique in this topic.

First we can think of each group of less than the relationship as a and to the edge, so we will also get a graph, we have to do is a bit of the graph, because we need to know where the contradiction, where we can get the unique sort, then we each input an edge (less than the relationship) to update the diagram, do a topological sort, See if we can get the only sort and contradictions. If all the edges are over and there is no contradiction, see if the sort is unique.
At the time of sorting we need to record the position of each point U (small row in front), the known from U as the starting point of the edge may have multiple (that is, has given a plurality of "u﹤xi" partial order relationship), then the position of the U point is the minimum value of all XI -1.

#include <iostream>#include <cstdio>#include <string.h>#include <cstring>#include <queue>#include <vector>using namespace STD;//v[i] Mark I-Letter whether DFS over, Sum[i] stores the number of letters in the first bit (to determine if the sort is unique)intN, M, v[ -], a[ -], sum[ -], k[ -]; vector<int>vec[ -];structegde{intx, y;} e[10010];intDfsintx) {intFlag =0; V[X] =-1;//the point being searched is marked as-1    intMi = n+1; for(inti =0; I < vec[x].size (); i++) {flag =1;inty = vec[x][i];if(V[y] <0)return 0;//Presence Ring        Else if(V[y] = =0)        {if(Dfs (y) = =0)return 0;Elsemi = min (mi, a[y]);    } mi = min (mi, a[y]); } V[x] =1;//Search points marked as 1    //a[x] Record the letter X ranked in the first few    if(Flag = =1)//flag=1 The point that there is known to be bigger than him, X is in front of the smallest position * * of all the points larger than itA[X] = Mi-1;ElseA[x] = n;//flag= 0, indicating that X is one of the largest known points, ranked in the last n-bit    return 1;}intTopu () {memset(V,0,sizeof(v)); for(inti =1; I <= N; i++) A[i] = n+2;//Initialize a bit of sort position for n+2;     for(inti =1; I <= N; i++) {if(V[i] = =0&& vec[i].size ()! =0)        {intTEM = DFS (i);if(TEM = =0)return 0; }    }return 1;}intMain () { while(scanf("%d%d", &n, &m)! = EOF && n && m) { for(inti =1; I <= m; i++) {CharA, B, C;intTemCin>> a >> c >> b; TEM = A-' A '+1; e[i].x = tem;//e[i] Stores the two points of the edge of the article ITEM = B-' A '+1;        E[I].Y = tem; }intError =-1;intFlag//Mark sort is for a         for(inti =1; I <= N; i++) {vec[i].clear ();} for(inti =1; I <= m; i++) {flag =1; Vec[e[i].x].push_back (E[I].Y);//continuously joins the edge and updates the VEC array of the storage edge each time the plus edge is added;Error = Topu ();//Each add edge is a topological sort, return value =0, there is contradiction, return value =1 when there is no contradiction            if(Error = =0)//have contradictions{printf("inconsistency found after%d relations.\n", i); Break; }Else if(Error = =1)            {memset(Sum,0,sizeof(sum)); Flag =1; for(inti =1; I <= N; i++) {if(A[i] = = n +2)//When the letter I is in the n+2 bit, the letter I is not connected to its edge,{flag =0;Continue; } sum[a[i]]++;//sum[i] Stores the number of letters in the I-bit (used to determine if a sort is unique)K[a[i]] = i;//k[i] Record the letter of the first digit                    if(Sum[a[i]) >1) {flag =0; Break;}//If there are more than one letter in the same position, there is a contradiction}if(Flag = =1)//If all points have an edge attached to it, and the position of the letter is not repeated, the sort is unique{printf("Sorted sequence determined after%d relations:", i); for(inti =1; I <= N; i++)printf("%c", K[i] +' A '-1);printf(". \ n"); Break; }            }        }if(Flag = =0)//If there is no contradiction, but the position of the letter is repeated, it indicates that the sort is not unique            printf("Sorted sequence cannot be determined.\n"); }return 0;}

nyoj349 poj1094 sorting It all out (topological sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.