Test instructions: (0 <= N <= 9)
Now there are N-Yin beads and N-yang beads, which form a ring between their yin and yang circles,
It is known that some yin beads and yang beads can not be placed in the adjacent position, or the sun bead will lose its effect,
Number of Yang beads with minimal loss of energy output
Analysis: Enumeration + binary graph matching
Put the beads in place first, then insert the Yangzhu into the vacancy.
Enumerates the positions of the beads and matches them to the position of the beads without affecting the sides.
So ans = min (n-Maximum number of matches)
Attention:
n = 0 o'clock cannot use the Next_permutation function!!
1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intmaxn= -;6 intN, m, ans;7 BOOLG[MAXN][MAXN], F[MAXN][MAXN], USED[MAXN];8 intLINKER[MAXN];9 intA[MAXN];Ten BOOLDfsintu) One { A for(intv =1; v <= N; v++) { - if(G[u][v] &&!Used[v]) { -USED[V] =1; the if(linker[v]==-1||DFS (Linker[v])) { -LINKER[V] =u; - return 1; - } + } - } + return 0; A } at intHungary () {//Hungary - intres =0; -memset (Linker,-1,sizeof(Linker)); - for(intU =1; U <= N; u++){ -memset (Used,0,sizeof(used)); - if(Dfs (U)) res++; in } - returnRes; to } + intMain () - { the while(~SCANF ("%d%d",&n,&m)) * { $memset (F,0,sizeof(F));Panax Notoginseng for(intI=1; i<=m;i++) - { the intb; +scanf"%d%d",&a,&b); Af[a][b]=1; the } +ans= -; - for(intI=1; i<=n;i++) a[i]=i; $ if(n = =0) {puts ("0");Continue; }//cannot be all arranged at this time!!! $ Do//Full arrangement - { -memset (G,0,sizeof(G)); the for(inti =1; I <= n;i++) - {Wuyi intL = A[i],r = (i = = n? a[1]: a[i+1]); the for(intU =1; u <= N; u++) - if( ! F[U][L] &&! F[U][R])//does not affect both sides Wug[u][i]=1; - } AboutAns=min (N-Hungary (), ans); $} while(Next_permutation (A +2, A + n +1));//because it's the ring, 1th don't move. -printf"%d\n", ans); - } -}
HDU 5727-necklace