UVA 818 (dfs+ graph + state compression)

Source: Internet
Author: User
Tags loop case

Test instructions: There are n rings, numbered from 1 to N, gives a number of interlocking situations, such as a and b for A and b two ring buckle together, each ring can be opened, ask at least how many rings open, and then buckle well, you can make all the ring into a chain.

Puzzle: State compression to all the open loop case enumeration out, and then take to determine whether to set up, update the image after opening the ring G[i][j], and the degree of each point, does not have three cases, 1. Calculates the number of degrees without opening the ring, if greater than 2 indicates that there will be no chain, 2. Take the ring without opening it. Visited on vis[i]++, if vis[i]>=2 illustrates the existence of the ring, 3. If the number of open rings num + 1 is less than the number of chains, the description cannot be linked to a chain. Find the minimum value output.

#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;const int N = 20;int vis[ N], N, Deg[n], g[n][n];void dfs (int u, int fa) {for (int i = 0; i < n; i++) {if (G[u][i] && i! = FA) {vis[i]++; if (Vis[i] < 2) DFS (i, u);}}}  BOOL Solve (int s, int num) {memset (deg, 0, sizeof (deg)), memset (Vis, 0, sizeof (VIS)), int link = 0;for (int i = 0; i < n; i++) {if (S & (1 << i)) {for (int j = 0; J < N; j + +) {if (G[i][j]) g[i][j] = G[j][i] = 0;}} for (int i = 0; i < n; i++) {if (!) ( S & (1 << i)) {for (int j = 0; J < N; j + +) if (G[i][j]) deg[i]++;if (Deg[i] > 2) return false;} for (int i = 0; i < n; i++) {if (!) ( S & (1 << i) &&!vis[i]) {link++;vis[i]++;d fs (i,-1);}} for (int i = 0; i < n; i++) if (Vis[i] >= 2) return false;if (link > num + 1) return False;return true; int main () {int f[n][n], cas = 1, A, B;while (scanf ("%d", &n) = = 1 && N) {memset (f, 0, sizeof (f)); while (scanf ("%d%d", &a, &b) && A! =-1) {f[a-1][b-1] = 1;f[b-1][a-1] = 1;} int res = 0x3f3f, tt = 1 << n;for (int i = 0; i < TT; i++) {for (int j = 0; J < N; j + +) for (int k = 0; K < N k++) g[j][k] = F[j][k];int temp = 0;for (int j = 0; J < N; j + +) if (I & (1 << j)) Temp++;if (Solve (i, temp)) res = min (res, temp);} printf ("Set%d:minimum links to Open is%d\n", cas++, res);} return 0;}


UVA 818 (dfs+ graph + state compression)

Related Article

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.