Sorted Sequence determined after 4 RELATIONS:ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
Topic: give you n points, give you M-bar to represent the size of the relationship. Ask you if you have a conflict (with a loop) after you join on the first few sides or you can determine the relationship, or you cannot determine the relationship.
Problem-solving ideas: The first time to join a side, the use of Floyd transmission closure, and then determine whether to form a ring. If there is no ring to determine whether the unique size relationship can be determined, here is an important criterion for determining if all nodes are equal to n-1, then the topological sort records the path.
#include <bits/stdc++.h>using namespace Std;int Map[50][50],indegree[50],outdegree[50];char S_ord[50];bool Floyd (int n) {for (int k=0;k<n;k++) {//transitive closure for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) { if (Map[i][k]&&map[k][j]) map[i][j]=1; }}} for (int i=0;i<n;i++)//Determine if ring if (Map[i][i]) return 1; return 0;} BOOL Calcu_is_ord (int n) {//calculation is currently ordered memset (indegree,0,sizeof (Indegree)); memset (outdegree,0,sizeof (Outdegree)); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (Map[i][j]) {indegree[j]++; outdegree[i]++; }}} for (int i=0;i<n;i++) {if (indegree[i]+outdegree[i]!=n-1) {/*) if all nodes satisfy the in-degree plus-out degree equals the total number of nodes minus one, the instructions are ordered. Because if ordered, will inevitably have the degree of 0~n-1, corresponding to the degree of n-1~0. So as long as all the nodes are n-1, it means that they are in order. */return 0; }} return 1;} void Topo_sort (int n) {//topological sort order int Que_[50],vis[50],top=0,cnt=0,u; for (int i=0;i<n;i++) {if (indegree[i]==0) {que_[++top]=i; }} memset (Vis,0,sizeof (VIS)); while (top) {u=que_[top--]; Vis[u]=1; s_ord[cnt++]=u+ ' A '; for (int i=0;i<n;i++) {if (!vis[i]&&map[u][i]) {indegree[i]--; } if (!vis[i]&&indegree[i]==0) {que_[++top]=i; }}} s_ord[cnt++]= ' ";} int main () {int n,m; Char str[10]; while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {memset (map,0,sizeof (MAP)); int flag_cir=0,flag_ord=0; Record the formation of a ring or an ordered for (int i=1;i<=m;i++) {scanf ("%s", str) on the first set of relationship inputs; map[str[0]-' A '][str[2]-' a ']=1; if (flag_cir| | Flag_ord) continue; if (Floyd (n)) {flag_cir=i;continue;} else if (Calcu_is_ord (n)) {topo_sort (n); flag_ord=i;continue;} } if (FLAG_CIR) printf ("inconsistency foundAfter%d relations.\n ", FLAG_CIR); else if (Flag_ord) {printf ("Sorted sequence determined after%d relations:%s.\n", Flag_ord,s_ord); }else{printf ("Sorted sequence cannot be determined.\n"); }} return 0;}