UVA-818 cutting Chains (bit compression + brute force search)

Source: Internet
Author: User

The main idea: a ring can open and close. Now there are N (1<=n<=15) numbered rings of 1~n are intricately connected, to open some loops to reconnect so that the N-rings can form a chain, ask at least several times to open the ring to achieve the purpose?

Title Analysis: The set of rings to be opened with a binary number, a total of 2^n cases, enumeration of each case. When the ring is opened, the ring is isolated, the next is to judge the rest of the ring is connected with a few rings, if some ring is still connected with more than two rings, the scheme is not feasible, it is impossible to form a chain, and then determine whether the remaining ring is connected to a circle, if there is, then the scheme is not feasible; After judging the first two conditions, all the rings must be in a short chain (length is less than or equal to N), just to determine whether the number of short chain is less than or equal to the number of open rings plus 1, if not, it must not even a chain, if established, then the scheme is feasible.

The code is as follows:

# include<iostream># include<cstdio># include<set># include<cstring># include<algorithm >using namespace Std;int n,ans,st[15],s[15],vis[15];int bitcount (int sta) {return Sta==0?0:bitcount (sta>>1) + (sta&1);} void Dfs (int u,int pre) {for (int i=0;i<n;++i) {if (i!=pre&&s[u]& (1<<i)) {++vis[i]            ;        if (vis[i]<2) DFS (I,U);    }}}bool ok (int sta) {for (int i=0;i<n;++i) s[i]=st[i];            Open ring for (int i=0;i<n;++i) {if (sta& (1<<i)) {s[i]=0;            for (int j=0;j<n;++j) {if (j!=i&&s[j]& (1<<i)) s[j]^= (1<<i); }}}///for (int i=0;i<n;++i) if (! (    sta& (1<<i)) &&bitcount (S[i]) >2) return false;    the circle int link=0;    memset (vis,0,sizeof (VIS)); for (int i=0;i<n;++i) {if (!vis[i]&&! ( sta& (1&LT;&LT;i)) {++link;            ++vis[i];        DFS (I,-1);    }} for (int i=0;i<n;++i) if (vis[i]>=2) return false;    Chain if (Link-1>bitcount (STA)) return false; return true;}    int main () {int a,b,cas=0;        while (scanf ("%d", &n) &&n) {memset (st,0,sizeof (ST));            while (scanf ("%d%d", &a,&b)) {if (a==-1&&b==-1) break;            St[a-1]|= (1<< (b-1));        St[b-1]|= (1<< (A-1));        } ans=n;        int tot=1<<n;        for (int i=0;i<tot;++i) if (ok (i)) ans=min (Ans,bitcount (i));    printf ("Set%d:minimum links to Open is%d\n", ++cas,ans); } return 0;}

  

UVA-818 cutting Chains (bit compression + brute force search)

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.