HDU5727 Necklace (Ring row + Hungary)

Source: Internet
Author: User

This question is the reference network each big gather the code to write out, no way I am too weak

Test instructions

Give you yin and yang beads each n, let you string into yin and yang and the string.

I'll give you a pair of M to show that a certain Yang bead will darken when it is adjacent to a yin bead.

Ask you at least how many yang beads are dimmed

Ideas:

I thought it might have something to do with the dichotomy, but there was no good idea.

I saw the puzzle on the Internet.

It is probably the first enumeration of all the n-1 of the yin Beads (ring arrangement)! Two

Then for each kind of arrangement to not darken the Yang beads and the yin beads to build the side run Hungary

Get the maximum match again with N-it

Ans Take the smallest one, yes.

/************************************************author:d evilcreated time:2016/7/20 17:8:19****************** ****************************** */#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<stdlib.h>using namespaceStd;typedefLong LongLL;Const intinf=0x3f3f3f3f;Const intn=Ten;intN,m,u,v,ans,pos[n],link[n];BOOLS[n][n],vis[n];vector<int>Eg[n];BOOLDfsintu) {     for(intI=0; I<eg[u].size (); i++)    {        intv=Eg[u][i]; if(!Vis[v]) {Vis[v]=1; if(link[v]==-1||DFS (Link[v])) {Link[v]=T; return 1; }        }    }    return 0;}voidsolve () {memset (s),0,sizeof(s));  while(m--) {scanf ("%d%d",&u,&v); S[U][V]=1; } ans=inf;  for(intI=1; i<=n; i++) Pos[i]=i;  Do{memset (link),-1,sizeof(link));  for(intI=1; i<=n; i++) {eg[i].clear ();  for(intj=1; j<=n; J + +) {u=Pos[i]; if(i==1) v=Pos[n]; Elsev=pos[i-1]; if(s[j][u]| | S[J][V])Continue;            Eg[i].push_back (j); }        }        intnow=0;  for(intI=1; i<=n; i++) {memset (Vis,0,sizeof(VIS)); now+=DFS (i); } ans=min (ans,n-Now ); }     while(Next_permutation (pos+2, pos+n+1) &&ans); printf ("%d\n", ans);}intMain () {//freopen ("In.txt", "R", stdin);     while(~SCANF ("%d%d",&n,&m)) {if(!N) {puts ("0"); Continue;    } solve (); }    return 0;}

HDU5727 Necklace (Ring row + Hungary)

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.