SRM 452 div2
Problems 1000 question: http://www.topcoder.com/stat? C = problem_statement & PM = 10572 & RD = 13906 answer: If there exist loop formed by required edges or node with degree> 2, then the answer is 0. if there are no loops and no nodes with degree> 2, then the graph will consist of S separate nodes and C chains. we need to count the number of ways to concatenate them to form a single path. we have (S + C )! Ways to order them, and for each chain we have 2 orientations, so the answer is (S + C )! * 2 ^ C: the answer is very understandable. The question is to determine whether a ring exists. # Include <iostream> # include <vector> # include <string> using namespace STD; bool vis [100]; int d [100]; vector <string> Road; int N; class effectonpath {void DFS (INT now) {vis [now] = true; For (INT I = 0; I <n; I ++) if (road [now] [I] = 'y ')&&(! Vis [I]) DFS (I);} public: int countpaths (vector <string> roads) {road = roads; memset (VIS, false, sizeof (VIS )); N = roads. size (); For (INT I = 0; I <n; I ++) {int S = 0; For (Int J = 0; j <n; j ++) if (roads [I] [J] = 'y') s ++; If (S> 2) return 0; d [I] = s ;} int tot1 = 0, tot2 = 0; For (INT I = 0; I <n; I ++) if ((! Vis [I]) & (d [I] <2) {DFS (I); If (d [I] = 0) tot1 ++; else tot2 ++;} For (INT I = 0; I <n; I ++) if (! Vis [I]) return 0; long ans = 1; for (long I = 0; I <tot1 + tot2; I ++) ans = (ANS * (I + 1) % 1000000007; For (INT I = 0; I <tot2; I ++) ans = (ANS * 2) % 1000000007; return ans ;}}; a wonderful answer. The deep-first search is used, and only the vertices with the degree of 1 and 0 are used as the start point of the Deep-first search, therefore, when the inner ring exists, no vertex is eligible to be the start point of the search, and then determines whether the entire point set space has been traversed. The idea is very simple, and the code writing is also very good! Remember this sentence !! You can find cycles using DFS.