Zju 1346 comparing your heroes state compression DP topological sorting count

Source: Internet
Author: User

When I was doing multiple schools, I met a question about how to calculate the number of topological sorting. I wrote it by the way.

Question:

A friend of yours is a fan of KOF. He has a comparison of the hero's strength and strength, so that you can rank these heroes based on these comparison relationships. How many ranking methods are available.

Ideas:

Use DP [s] to record the current status. S indicates the set of vertices that have been sorted in the current phase of topological sorting. Then we can enumerate the vertices in the current sorting. The transfer condition is that all the founders of this vertex are sorted, and this vertex is not sorted. Then the transfer is done, and the final state is that all vertices are sorted.

Code:

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # include <string> 8 # include <queue> 9 # include <stack> 10 # include <vector> 11 # include <map> 12 # include <set> 13 # include <functional> 14 # include <time. h> 15 16 using namespace STD; 17 18 const int INF = 1 <30; 19 const int maxn = 16; 20 21 int DP [1 <maxn]; 22 int pre [maxn]; // note Record the precursor set of each vertex 23 char name [maxn] [maxn]; // record the name of each character 24 char STR [2] [maxn]; 25 int n, m; 26 27 void input () {28 // read data and Its Relationship. 29 // save them and give each name a number 30 // process the 31 int U, V; 32 N = 0 in the PRE; 33 memset (PRE, 0, sizeof (pre); 34 for (INT I = 0; I <m; I ++) {35 scanf ("% S % s ", STR [0], STR [1]); 36 37 for (u = 0; U <n; U ++) // find this string 38 If (strcmp (name [u], STR [0]) = 0) 39 break; 40 if (u = N) strcpy (name [n ++], STR [0]); // if not found, insert this string 41 42 for (V = 0; v <n; V ++) 43 If (strcmp (name [v], STR [1]) = 0) 44 break; 45 if (V = N) strcpy (name [n ++ ], STR [1]); 46 47 pre [v] | = (1 <u); // U is the precursor of V, so, add u to V's precursor set 48} 49 // For (INT I = 0; I <n; I ++) printf ("% d", pre [I]); puts (""); // output precursor set 50} 51 52 void solve () {53 // dynamic planning 54 // enumerate each State (a set with topological order already exists ), then enumerate the point 55 memset (DP, 0, sizeof (DP) of this set; 56 DP [0] = 1; // initial status 57 58 for (int s = 0; S <(1 <n); s ++) if (DP [s]! = 0) {// pruning 59 for (INT I = 0; I <n; I ++) if (S & Pre [I]) = pre [I]) &! (S & (1 <I) {// The precursor of I is in this state, and I is not in 60 DP [S | (1 <I)] + = DP [s]; 61} 62} 63 64 printf ("% d \ n", DP [(1 <n)-1]); // all vertices are sorted in the final state 65} 66 67 int main () {68 # ifdef phantom0169 freopen ("zju1346.txt", "r", stdin ); 70 # endif // phantom0171 72 while (scanf ("% d", & M )! = EOF) {73 input (); 74 solve (); 75} 76 77 return 0; 78}
Zju 1346

 

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.