# Sha 818 cutting chains (DFS)

Source: Internet
Author: User

What a find! Anna Locke has just bought several links of chain some of which may be connected. they are made from zorkium, a material that was frequently used to manufacture jewelry in the last century, but is not used for that purpose anymore. it has its very own shine, incomparable to gold or silver, and impossible to describe to anyone who has not seen it first hand.

Anna wants the pieces joined into a single end-to-end strand of chain. she takes the links to a jeweler who tells her that the cost of joining them depends on the number of chain links that must be opened and closed. in order to minimize the cost, she carefully calculates the minimum number of links that have to be opened to rejoin all the links into a single sequence. this turns out to be more difficult than she at first thought. you must solve this problem for her.

Input

The input consists of descriptions of sets of chain links, one set per line. each set is a list of integers delimited by one or more spaces. every description starts with an integer N, which is the number of chain links in the Set, where 1 ≤ n ≤ 15. we will label the links 1, 2 ,..., n. the integers following n describe which links are connected to each other. every connection is specified by a pair of integers I, j where 1 ≤ I, j ≤ n and I =j, indicating that chain links I and j are connected, I. E ., one passes through the other. the description for each set is terminated by the pair-1-1, which shoshould not be processed.

The input is terminated by a description Starting with n = 0. This description shoshould not be processed and will not contain in data for connected links.

Output

For each set of chain links in the input, output a single line which reads

Set N: Minimum links to open is m

Where N is the set number and m is the minimal number of links that have to be opened and closed such that all links can be joined into one single chain.

Sample Input Output for the sample input
`5 1 2 2 3 4 5 -1 -17 1 2 2 3 3 1 4 5 5 6 6 7 7 4 -1 -14 1 2 1 3 1 4 -1 -13 1 2 2 3 3 1 -1 -13 1 2 2 1 -1 -10`
`Set 1: Minimum links to open is 1Set 2: Minimum links to open is 2Set 3: Minimum links to open is 1Set 4: Minimum links to open is 1Set 5: Minimum links to open is 1`
Question: a ring is incomplete. A complete ring can be formed only when there are at least a few open rings.

`# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> typedef long ll; using namespace STD; # define repf (I, a, B) for (INT I = A; I <= B; ++ I) # define rep (I, n) for (INT I = 0; I <n; ++ I) # define clear (A, x) memset (A, X, sizeof) int MP [15] [15]; int vis [15]; int N, CNT; bool OK (INT S) // OK determines the illegal state of a ring connected to more than two, {rep (I, n) {If (S & (1 <I) continue; Int num = 0; rep (J, n) {If (S & (1 <j) continue; If (MP [I] [J]) num ++ ;} if (Num> 2) return true;} return false;} bool DFS (int s, int now, int pre) {vis [now] = 1; rep (I, n) {If (! MP [now] [I] | (S & (1 <I) | I = pre) continue; If (vis [I]) return true; if (DFS (S, I, now) return true;} return false;} bool circle (INT s) // The Circle is regarded as an invalid state. {rep (I, n) {If (vis [I] | (S & (1 <I) continue; CNT ++; If (DFS (S, I,-1 )) return true;} return false;} int CAL (INT s) // calculate the number of open rings {return S = 0? 0: Cal (S> 1) + (S & 1) ;}int solve () {int ans = 0x3f3f3f; int status = 1 <n; rep (STAU, status) // enumeration status 1: Indicates open {CNT = 0; clear (VIS, 0); If (OK (STAU) | circle (STAU) continue; // OK to judge the illegal state of a ring connected to more than two, circle to determine the illegal state of the ring if (CAL (STAU)> = cnt-1) ans = min (ANS, Cal (STAU);} return ans;} int main () {int X, Y; int CAS = 1; while (~ Scanf ("% d", & N) {clear (MP, 0); While (~ Scanf ("% d", & X, & Y) & (X! =-1 & Y! =-1) MP [x-1] [Y-1] = MP [Y-1] [x-1] = 1; printf ("set % d: minimum links to open is % d \ n ", CAS ++, solve ();} return 0 ;}`

Sha 818 cutting chains (DFS)

Related Keywords:

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.