Va 818 Cutting Chains (DFS), chainsdfs
What a find! Anna Locke has just bought several links of chain some of which may be connected. they are made from zorkium, a material that was frequently used to manufacture jewelry in the last century, but is not used for that purpose anymore. it has its very own shine, incomparable to gold or silver, and impossible to describe to anyone who has not seen it first hand.
Anna wants the pieces joined into a single end-to-end strand of chain. she takes the links to a jeweler who tells her that the cost of joining them depends on the number of chain links that must be opened and closed. in order to minimize the cost, she carefully calculates the minimum number of links that have to be opened to rejoin all the links into a single sequence. this turns out to be more difficult than she at first thought. you must solve this problem for her.
Input
The input consists of descriptions of sets of chain links, one set per line. each set is a list of integers delimited by one or more spaces. every description starts with an integer n, which is the number of chain links in the set, where 1 ≤ n ≤ 15. we will label the links 1, 2 ,..., n. the integers following n describe which links are connected to each other. every connection is specified by a pair of integers I, j where 1 ≤ I, j ≤ n and I =j, indicating that chain links I and j are connected, I. e ., one passes through the other. the description for each set is terminated by the pair-1-1, which shoshould not be processed.
The input is terminated by a description starting with n = 0. This description shoshould not be processed and will not contain in data for connected links.
Output
For each set of chain links in the input, output a single line which reads
Set N: Minimum links to open is M
Where N is the set number and M is the minimal number of links that have to be opened and closed such that all links can be joined into one single chain.
Sample Input |
Output for the Sample Input |
5 1 2 2 3 4 5 -1 -17 1 2 2 3 3 1 4 5 5 6 6 7 7 4 -1 -14 1 2 1 3 1 4 -1 -13 1 2 2 3 3 1 -1 -13 1 2 2 1 -1 -10 |
Set 1: Minimum links to open is 1Set 2: Minimum links to open is 2Set 3: Minimum links to open is 1Set 4: Minimum links to open is 1Set 5: Minimum links to open is 1 |
Question: a ring is incomplete. A complete ring can be formed only when there are at least a few open rings.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> typedef long LL; using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof) int mp [15] [15]; int vis [15]; int n, cnt; bool OK (int s) // OK determines the illegal state of a ring connected to more than two, {REP (I, n) {if (s & (1 <I) continue; Int num = 0; REP (j, n) {if (s & (1 <j) continue; if (mp [I] [j]) num ++ ;} if (num> 2) return true;} return false;} bool dfs (int s, int now, int pre) {vis [now] = 1; REP (I, n) {if (! Mp [now] [I] | (s & (1 <I) | I = pre) continue; if (vis [I]) return true; if (dfs (s, I, now) return true;} return false;} bool circle (int s) // The circle is regarded as an invalid state. {REP (I, n) {if (vis [I] | (s & (1 <I) continue; cnt ++; if (dfs (s, I,-1 )) return true;} return false;} int cal (int s) // calculate the number of open rings {return s = 0? 0: cal (s> 1) + (s & 1) ;}int solve () {int ans = 0x3f3f3f; int status = 1 <n; REP (stau, status) // enumeration status 1: Indicates open {cnt = 0; CLEAR (vis, 0); if (OK (stau) | circle (stau) continue; // OK to judge the illegal state of a ring connected to more than two, circle to determine the illegal state of the ring if (cal (stau)> = cnt-1) ans = min (ans, cal (stau);} return ans;} int main () {int x, y; int cas = 1; while (~ Scanf ("% d", & n) {CLEAR (mp, 0); while (~ Scanf ("% d", & x, & y) & (x! =-1 & y! =-1) mp [x-1] [Y-1] = mp [Y-1] [x-1] = 1; printf ("Set % d: minimum links to open is % d \ n ", cas ++, solve ();} return 0 ;}
Sha 10599 Robots (II) -- Chapter Dp of Liu rujia -- Interpretation of the standard process --
The biggest feature of this program is to store each target point into an array without creating an adjacent matrix.
Why is LIS required. This seems that the route is not obvious, and LIS can implement nlogn using stacks. In this case, you can perform a memory-based search to traverse the vertices with a large serial number and determine whether they are in the lower right corner.