UVA 818 (bit op + ring)

Source: Internet
Author: User

Cutting Chains

What a find! Anna Locke has just bought several links of chain some of which to be connected. They is made from zorkium, a material that were frequently used to manufacture jewelry in the last century D for that purpose anymore. It has a very own shine, incomparable to gold or silver, and impossible to describe to anyone who have 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, the cost of joining them depends on the number of chain links that Mus t be opened and closed. In order to minimize the cost, she carefully calculates the minimum number of links that has to is opened to rejoin all T He links to a single sequence. This turns is more difficult than she at first thought. You must solve this problem to her.

Input

The input consists of descriptions of sets of chain links, one set per line. Each set are a list of integers delimited by one or more spaces. Every description starts with an integer n, which are the number of chain links in the set, where 1≤n≤15. We'll label the links 1, 2,..., N. The integers following n describe which links is connected to each other. Every connection is specified by a pair of integers i,j where 1≤i,j≤n and I≠j, indicating this chain links I and J are Connected, i.e, one passes through the other. The description for each set are terminated by the pair-1-1, which should isn't be processed.

The input is terminated by a description starting with n = 0. This description should isn't be processed and won't contain 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 are M

Where N is the set number and M are the minimal number of links that has opened and closed such that all links can b E joined to 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 are 1Set 2:minimum links to open are 2Set 3:minimum links to open are 1Set 4:minimum links to Open is 1Set 5:minimum links to open is 1

ACM World Finals, problem C

Test instructions is very difficult to understand, the last to understand that there are n rings, numbering from 1 to N, give a number of interlocking situations, such as to 1 and 2 means that 1 and 22 rings together, each ring can be opened, ask at least how many rings to open, and then buckle well, you can make all the ring into a chain.

Exercises

Because n maximum is 15, you can use a binary number to indicate whether each ring is opened, and then the ring is not opened to determine if there is a position of more than 2, and whether there is a ring, and to ensure that the number of open rings plus 1 is greater than the number of remaining chains.

It's silly to forget the ring number starting from 1, WA has been countless times ...

#include <cstdio>#include<iostream>#include<sstream>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<algorithm>using namespacestd;#definell Long Long#define_cle (M, a) memset (M, A, sizeof (m))#defineRepu (I, A, b) for (int i = A; I < b; i++)#defineREPD (I, A, b) for (int i = b; i >= A; i--)#defineSFI (n) scanf ("%d", &n)#definePFI (n) printf ("%d\n", N)#defineSfi2 (n, m) scanf ("%d%d", &n, &m)#definePfi2 (n, m) printf ("%d%d\n", N, m)#definePfi3 (A, B, c) printf ("%d%d%d\n", A, B, c)#defineMAXN 16#defineR 6#defineC 7Const intINF =0x3f3f3f3f; Vector<int>V[MAXN];BOOLVIS[MAXN];BOOLMP[MAXN][MAXN];BOOLOPEN[MAXN];BOOLDfsintRintFA) {    if(Vis[r])return true; VIS[R]=1; intSiz =v[r].size (); intD =siz; Repu (i,0, Siz) {        if(Open[v[r][i]]) d--; Else if(V[r][i]! =FA)if(Dfs (V[r][i], R))return true; }    if(D >2)return true; return false;}intMain () {intN; intKase =0;  while(~SFI (n) &&N) {Kase++; _cle (MP,0); intx, y;  while(1) {sfi2 (x, y); if(x = =-1&& y = =-1) Break; MP[Y][X]= Mp[x][y] =1; } Repu (I,1, n +1) {v[i].clear (); Repu (J,1, n +1)if(Mp[i][j]) V[i].push_back (j); }        intMinn =N; intLim =1<<N; intFlag; Repu (i,0, Lim) {Flag=0; _cle (Vis,0); _cle (Open,0); inttot =0, t =0; Repu (J,0, N)if((1&LT;&LT;J) & i) tot++, Open[j +1] =1;//as a result of this J + 1 I've been 1,wa countless times.Repu (J,1, n +1)               if(!open[j] &&!Vis[j]) {T++; if(Dfs (J,-1)) {flag =1; Break; } }            if(!flag && t <= tot +1) Minn =min (TOT, Minn); } printf ("Set%d:minimum links to open is%d\n", Kase, Minn); }    return 0;}
View Code

UVA 818 (bit op + ring)

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.