Poj 1094 Problem Solving report

Source: Internet
Author: User

Poj 1094 Problem Solving report

This topic mainly involves the use of topological sorting, which can be seen by any algorithm book. It is not difficult, so it is not difficult.

I use an adjacent table to save the graph, and use DM and DN to save the outbound and inbound degrees of each node (that is, a letter) respectively.

I just want to emphasize the following:

1. first determine whether a loop exists

At the beginning, I directly returned the result when I detected that the sequence could not be determined, but did not determine whether there was a loop. The result was always wa;

2. When the sequence can be determined or the loop is detected, the following input is ignored.

Code:
  1. # Include <iostream>
  2. # Include <fstream>
  3. Using namespace STD;
  4. Char G [26] [26];
  5. Int DM [26]; // the sum of each column, that is, the inbound degree of each letter
  6. Int DN [26]; // The sum of each row, that is, the exit degree of each letter
  7. Int M, N;
  8. Char result [27];
  9. Int toposort ()
  10. {
  11. Int index; // start point
  12. Int r = 0;
  13. Int count;
  14. Bool sorted = true;
  15. Int DMT [26];
  16. Int DNT [26];
  17. Memcpy (DMT, DM, sizeof (DMT ));
  18. Memcpy (DNT, dn, sizeof (DNT ));
  19. // Find a point with zero degree of access
  20. For (Int J = 0; j <n; j ++)
  21. {
  22. Count = 0;
  23. For (INT I = 0; I <n; I ++)
  24. {
  25. If (0 = DMT [I])
  26. {
  27. Index = I;
  28. Count ++;
  29. }
  30. }
  31. If (0 = count) // discovers the loop
  32. Return count;
  33. If (count> 1) Sorted = false;
  34. For (INT I = 0; I <DNT [Index]; I ++)
  35. {
  36. DMT [G [Index] [I]-'a'] --;
  37. }
  38. DMT [Index] =-1;
  39. Result [R ++] = index + 'a ';
  40. }
  41. Result [R] = 0;
  42. If (sorted)
  43. Return 1; // set the order **
  44. Else
  45. Return 2; // The sequence is uncertain.
  46. }
  47. Int main ()
  48. {
  49. Int I, J, K;
  50. Char STR [4];
  51. // Ifstream in ("data. In ");
  52. Cin> N> m;
  53. While (N & M)
  54. {
  55. K = 0;
  56. Int found = 0; // The sequence can be completely determined.
  57. Int incons = 0; // finds a conflict, that is, a loop.
  58. Memset (DM, 0, sizeof (DM ));
  59. Memset (DN, 0, sizeof (DN ));
  60. Memset (G, 0, sizeof (g ));
  61. For (I = 0; I <m; I ++)
  62. {
  63. Cin> STR;
  64. If (! Found &&! Incons)
  65. {
  66. For (j = 0; j <DN [STR [0]-'a']; j ++)
  67. {
  68. If (G [STR [0]-'a'] [J] = STR [2])
  69. Break;
  70. }
  71. If (j = DN [STR [0]-'a'])
  72. {
  73. G [STR [0]-'a'] [J] = STR [2];
  74. DM [STR [2]-'a'] ++;
  75. DN [STR [0]-'a'] ++;
  76. }
  77. Int res = toposort ();
  78. If (1 = res) // The sequence is determined.
  79. {
  80. Found = I + 1;
  81. }
  82. Else if (0 = res) // finds a conflict
  83. {
  84. Incons = I + 1;
  85. }
  86. }
  87. }
  88. If (found)
  89. {
  90. Cout <"sorted sequence determined after" <found <"relations:" <result <"." <Endl;
  91. }
  92. Else if (incons)
  93. {
  94. Cout <"inconsistency found after" <incons <"relations." <Endl;
  95. }
  96. Else
  97. {
  98. Cout <"sorted sequence cannot be determined." <Endl;
  99. }
  100. Cin> N> m;
  101. }
  102. Return 0;
  103. }

 

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.