The main topic: give n rings (like the Olympic rings of the kind of ring), you need to open the M ring, and then the M-ring as the intermediary, so that all the ring can form a chain
Problem-solving ideas: Brute force enumeration, with binary representation of the position and number of disconnected rings.
After disconnecting the ring, it is disconnected from the ring, that is, the ring becomes an isolated ring
Then judge whether the non-disconnected ring can be connected to a ring, if it can be connected to a ring, it is not possible to pass through the M-ring when the intermediary linked to a chain
Also have to judge the degree of the non-disconnected ring, if the degree of more than 2, can not become a chain
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define MAXNintG[MAXN][MAXN], M[MAXN][MAXN], VIS[MAXN], n, ans;intBitcount (ints) {returnS? S%2+ Bitcount (S/2):0;}voidDfsintCurintf) { for(inti =0; I < n; i++)if(I! = f && m[cur][i]) {vis[i]++;if(Vis[i] <2) Dfs (i,cur); }}BOOLJudgeintS) {intD[MAXN], broke =0, link =0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +) M[i][j] = G[i][j];memset(Vis,0,sizeof(VIS));memset(d,0,sizeof(d));//Remove the segments connected to the broke beads for(inti =0; I < n; i++)if((1<< i) & S) {broke++; for(intj =0; J < N; J + +) M[i][j] = m[j][i] =0; }//Record the degree of beads not broke for(inti =0; I < n; i++)if(! ((1<< i) & S)) { for(intj =0; J < N; J + +)if(M[i][j]) d[i]++;if(D[i] >2)return false; }//Handle whether each string will form a ring and a total of several strings for(inti =0; I < n; i++)if(!vis[i] &&! ((1<< i) & S)) {link++; vis[i]++; DFS (i,-1); } for(inti =0; I < n; i++)if(Vis[i] >=2)return false;if(Link-1> Broke)return false;return true;}voidSolve () {ans = -;intEnd = (1<< N); for(inti =0; i < End; i++)if(Judge (i)) ans = min (ans, bitcount (i));}intMain () {intCAS =1; while(scanf("%d", &n) = =1&& N) {intx, y;memset(g,0,sizeof(g)); while(scanf("%d%d", &x, &y)) {if(x = =-1&& y = =-1) Break; G[x-1][y-1] = G[y-1][x-1] =1; } solve ();printf("Set%d:minimum links to Open is%d\n", cas++, ans); }return 0;}
UVA-818 Cutting Chains violence