ZJU1346 Comparing Your Heroes-count of topological sorting★

Source: Internet
Author: User

Description:

Someone wants to sort the figures of the champion. Enter N, And the next n rows have two strings a B, indicating that a is better than B. The number of different Topology Sorting methods is required. If the sorting output cannot be completed, 0 is returned.

 

Analysis:

AvailableDynamic Planning.

For the topological sorting of N nodes, set the current M nodes with 0 degrees as Zi. SoCurrently, you can select any of the nodes whose m degrees are 0 and the number of sorting methods for the remaining nodes is irrelevant to the previous one.

So you can useNode Set indicates the status. State Transfer: removes a node with a degree of 0 from the current set.

So,Total count of State S = Total number of Zi removed from S by Σ.

When only one node is left, the number of sorting methods is 1.

/* Zju1346 comparing your heroes */# include
 
  
# Include
  
   
# Define N 20 # define M 100001 # define CLR (a) memset (A, 0, sizeof (A) int N; int hash [m]; int a [n] [N]; int DP [m]; int error; int elfhash (char * Key) {unsigned long h = 0; while (* key) {H = (H <4) + * Key ++; unsigned long G = H & 0xf0000000l; If (g) H ^ = G> 24; H & = ~ G;} return H % m;} int ID (char s []) {int H = elfhash (s); If (! Hash [H]) hash [H] = ++ N; return hash [H];} int encode (int e []) {int I, C = 0; for (I = 1; I <= N; I ++) {If (E [I]) c | = 1 <(I-1);} return C ;} int count (int e []) {int c = encode (E); If (DP [c]) return DP [c]; If (error) return 0; int I, j, num = 0; int Z [N], pre, m = 0; for (I = 1; I <= N; I ++) {If (E [I]) {num ++; Pre = 0; For (j = 1; j <= N; j ++) if (E [J] & A [J] [I]) Pre ++; If (! PRE) Z [M ++] = I ;}} if (! M) {// Topology Sorting cannot be completed error = 1; return 0;} If (num = 1) {// only one node DP [c] = 1; return DP [c];} int sum = 0; for (I = 0; I <m; I ++) {e [Z [I] = 0; sum + = count (e); E [Z [I] = 1; if (error) return 0;} DP [c] = sum; return DP [c];} int main () {int m; while (scanf ("% d", & M )! = EOF) {// init n = 0; CLR (hash); CLR (a); CLR (DP); // input int I, J, K; char s [N], t [N]; for (k = 0; k <m; k ++) {scanf ("% S % s", S, T ); I = ID (s); j = ID (t); A [I] [J] = 1;} // DP int e [N]; for (I = 1; I <= N; I ++) E [I] = 1; error = 0; count (E); // output k = encode (E); If (error) printf ("0/N"); else printf ("% d/N", DP [k]);} 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.