POJ 1270 following Orders (topological sort +dfs)

Source: Internet
Author: User

Approximate test instructions: Each sample contains two lines, the first line is entered n characters, and may be unordered. The second line is entered in pairs of a B, representing a to precede B. All of the outputs conform to such sequences.


Idea: A very obvious sort of topology. To output all of the sequences, DFS is performed from a point with a degree of 0, each time selecting a point with an entry level of 0, adding an output sequence and reducing the degree of the point adjacent to it. After Dfs is over, change the status back.

#include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector># Include <math.h> #include <string.h> #include <stack> #include <queue> #define LL long long# Define _ll __int64using namespace Std;const int maxn = 110;char contents[30];bool mapp[60][60];int n = 0;char t,t1,t2;int            Flag;int in[30];int vis[30];int ans[30];void dfs (int num) {if (num = = N) {for (int i = 0; i < n; i++)        printf ("%c", Ans[i]);        printf ("\ n");    Return } for (int i = 0; i < n; i++) {if (!vis[i] &&!in[contents[i]-' A "]) {for (int j = 0; J < N; J + +) {if (mapp[contents[i]-' a '][contents[j]-' a ']) {in[Conten                ts[j]-' a ']--;            }} Ans[num] = Contents[i];                        Vis[i] = 1;                        DFS (NUM+1); Note Change the status back to for (int j = 0; J < N J + +) {if (mapp[contents[i]-' a '][contents[j]-' a ']) {in[Conten                ts[j]-' a ']++;        }} Vis[i] = 0;    }}}int Main () {flag = 0;            while (~SCANF ("%c", &t)) {if (T >= ' a ' && t <= ' z ') {contents[n++] = t;        Continue        } else if (t = = ") continue;            else {if (flag) printf ("\ n");            flag = 1; Sort (contents,contents+n);            Because the input may be unordered, sort memset (in,0,sizeof (in)) first;            memset (mapp,false,sizeof (MAPP));            memset (vis,0,sizeof (VIS));                while (~scanf ("%c%c", &t1,&t2)) {mapp[t1-' a '][t2-' a '] = 1;                in[t2-' a ']++;            if (getchar () = = ' \ n ') break;            } dfs (0);        n = 0; }} return 0;}







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.