UVa 1572 self-assembly (topological sort)

Source: Internet
Author: User

Topic Links:

https://cn.vjudge.net/problem/UVA-1572

Automatic Chemical Manufacturing is experimenting with a process called self-assembly. In this process, molecules with natural affinity for each other is mixed together in a solution and allowed to Spontaneou Sly assemble themselves into larger structures. But there was one problem:sometimes molecules assemble themselves into a structure of unbounded size, which gums up the MA Chinery. Must write a program to decide whether a given collection of molecules can is assembled into a structure of unbounded Size. You should make simplifying assumptions:1) The problem are restricted to the dimensions, and 2) each molecule in the C Ollection is represented as a square. The four edges of the square represent the surfaces on which the molecule can connect to other compatible molecules. In the each test case, you'll be given a set of molecule descriptions. Each type of molecule are described by four Two-character connector labels that indicate how their edges can connect to the E Dges of other MoleCules. There is types of connector labels:
An uppercase letter (A, ..., Z) followed by + or-. Edges is compatible if their labels has the same letter but different signs. For example, A + is compatible with a-but are not compatible with a + or B.
Both zero digits 00. An edge with the This label is not a compatible with any edge (not even with another edge labeled 00).
Assume there is a unlimited supply of molecules of each type, which could be rotated and reected. As the molecules assemble themselves into larger structures, the edges of both molecules may is adjacent to each and only If they is compatible. It is permitted for a edge, regardless of its connector label,to being connected to nothing (no adjacent molecule on that Ed GE). Figure A.1 shows an example of three molecule types and A structure of bounded size that can is assembled from them (other Bounded structures is also possible with this set of molecules).

1 /*2 Test Instructions Description:3 give the card of N square, there is a sign from the east counter-clockwise four side, when the two cards on the side of the same letter and the symbol is different when the two cards can be connected, asked whether the4 Several cards form an infinitely large structure. 5 Problem Solving Ideas:6 test instructions more difficult to understand, rationale, infinite structure, is similar to the middle of a molecular structure can be infinitely added, then the label as a node, a total of 26 * 2,7 the individual cards are considered to be a kind of relationship, constituting a direction graph, if there is a forward ring in the direction graph, it can constitute an infinitely large structure. Then the problem is turned into,8 in order to determine whether there is a forward ring problem in a graph, do a topological sort, if there is a topological sequence, there is no forward ring, if there is no topological sequence, there is a forward ring. 9 The idea of queue solution for topological sortingTen According to the graph to calculate the degree and the degree of each node, the degree of 0 of the node into the queue, and then into the loop once out of the team, and it has a relationship with the Node B in the degree of a minus one, One if the penetration of Node B is reduced to exactly 0, then Node B is also queued until the queue is empty. If the number of nodes and the total number of the outbound is equal to indicate the existence of a topological sequence, there is no ring. But A because of the relationship between the side of the special, there is no way according to the vertex of the degree of 0, to determine the queue, because there are four symbols on a card, statistics into the degree and out of the time must be combined to draw off - system, the recursive method of topological ordering is adopted to increase the degree of penetration.  - recursive solution of topological sort the with the help of an array of tokens c,c[u]=0 means that never have visited u,c[u]=1 to indicate that it has been visited, and has been recursively accessing all descendants of it, c[u]=-1 means that it is being accessed and - that is, its descendants are being accessed, and DFS (U) is in the stack frame and has not yet returned.  - */  -#include <bits/stdc++.h> + using namespacestd; -  + Const intmaxn= the+Ten; A BOOLMP[MAXN][MAXN]; at intC[MAXN]; - intcycle (); - intDfsintu); -  - intIdCharA1,CharB1) { -     return(a1-'A')*2+ b1=='+'?0:1; in } - voidConnectCharA1,CharB1,CharA2,CharB2) { to     if(A1 = ='0'|| B1 = ='0') +         return; -     intU=id (A1,B1) ^1, v=ID (A2,B2); themp[u][v]=1; * } $ Panax Notoginseng intMain () - { the     intN; +     CharLIST[MAXN]; A     //freopen ("E:\\testin.txt", "R", stdin); the      while(SCANF ("%d", &n)! =EOF) { +Memset (MP,0,sizeof(BOOL) *maxn*MAXN); -          for(intI=0; i<n;i++){ $scanf"%s", list); $              -              for(intI=0;i<4; i++){ -                  for(intj=0;j<4; j + +){ the                     if(I! = j) Connect (list[i*2],list[i*2+1],list[j*2],list[j*2+1]); -                 }Wuyi             } the         } -          Wu         if(cycle ()) -printf"bounded\n"); About         Else $printf"unbounded\n"); -     } -     return 0; - }  A  + intcycle () { theMemset (c,0,sizeof(int)*MAXN); -      for(intu=0;u< the; u++){ $         if(C[u] = =0&&dfs (U)) {     the             return 1; the         } the     } the     return 0; - }  in  the intDfsintu) { thec[u]=-1; About      for(intv=0;v< the; v++){ the         if(Mp[u][v]) { the             if(C[v] = =-1) the                 return 1;//There is a forward ring +             Else -                 if(C[v] = =0&&Dfs (v)) the                     return 1; Bayi         } the     } thec[u]=1; -     return 0; -}

UVa 1572 self-assembly (topological sort)

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.