SRM 452 div2 problems 1000

Source: Internet
Author: User

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.

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.