# Poj1270 topological sorting

Source: Internet
Author: User

Meaning: given some relations of size, the relationships are sorted in lexicographically ascending order. For example, if X <Y & x <z, XYZ and xzy are met.

Idea: we can create a directed edge for each group of large-size relationships to form a directed graph, and then perform topology on the directed graph. The result is satisfied. However, the topology is a set of feasible solutions, and the meaning is to output all feasible solutions, which can be traced back when the topology is obtained recursively. In this way, all feasible solutions can be output. However, sort all the variables.

Code:

`# Pragma warning (Disable: 4786) # include <cstdio> # include <cstring> # include <algorithm> # include <map> const int maxn = 200; char var [maxn]; char ans [maxn]; char st [maxn]; int map [maxn] [maxn]; int N; int indeg [maxn]; STD: Map <char, int> hash; void toposort (int K) // topological backtracking {If (k = N) {ans [N] = '\ 0 '; printf ("% s \ n", ANS); Return ;}for (INT I = 0; I <n; I ++) {Int J; if (indeg [I] = 0) {indeg [I] --; ans [k] = sT [I]; for (j = 0; j <N; j ++) {If (Map [I] [J]) indeg [J] --;} toposort (k + 1); indeg [I] ++; for (j = 0; j <n; j ++) if (Map [I] [J]) indeg [J] ++ ;}} void Init () {n = 0; memset (indeg, 0, sizeof (indeg); memset (MAP, 0, sizeof (MAP);} void work () {char C [3]; int I; int L = strlen (VAR), k = 0; Init (); for (I = 0; I <L; I ++) {If (VAR [I]> = 'A' & var [I] <= 'Z') ST [n ++] = var [I];} STD :: sort (St, ST + n); for (I = 0; I <n; I ++) {hash [st [I] = I;} gets (VAR ); L = strlen (VAR); for (I = 0; I <L; I ++) {If (VAR [I]> = 'A' & var [I] <= 'Z') {C [K % 2] = var [I]; if (K % 2 = 1) {map [hash [C [0] [hash [C [1] = 1; indeg [hash [C [1] ++;} k ++;} toposort (0); printf ("\ n");} int main () {While (gets (VAR) {work ();} return 0;}/* a B f Ga B FV w x y ZV Y x V Z v w v */`

Related Keywords:

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.