Graph structure Exercise--judging whether a given graph has a valid topological sequence Time limit:1000ms Memory limit:65536k The topic describes whether there is a valid topological sequence for a given graph to determine if there is one. The input input contains multiple groups, each set in the following format. The first line contains two integer n,m that represent the number of vertices and sides of the graph. (n<=10) The M-line is followed by two integers a b per line, indicating that there is a forward edge from A to B. Output Yes if there is a valid topological sequence for the given graph, otherwise output no. Sample input
1 02 21) 22 1
Sample output
YESNO
Hint Source Zhaolijiang
Sample Program
#include <stdio.h> #include <string.h> #include <stdlib.h>int n,m;int map[20][20];int in[20];void Topo () {int i,j,k; int flag; for (i=1;i<=n;i++) {flag=0; for (j=1;j<=n;j++) {if (in[j]==0) {flag=1;//As long as the point with zero-in degrees becomes 1 in [j]=-1;//Delete this node for (k=1;k<=n;k++)//The junction of the nodes connected to the degree -1 {if (map[j][k]) in[k]--; } break;//found a zero-in to the end of the} if (flag==0)//If the node is not output, there is no zero in the figure of the junction, proving that there is a ring, it is not legal BR Eak } if (flag==1) printf ("yes\n"); else printf ("no\n");} int main () {int u,v,i,j; while (~SCANF ("%d%d", &n,&m)) {memset (map,0,sizeof (map)); memset (In,0,sizeof (in)); while (m--) {scanf ("%d%d", &u,&v); Map[u][v]=1; in[v]++; } topo (); } return 0;}
Graph structure Exercise--determining whether a given graph has a valid topological sequence (topological sort judgment ring)