Graph structure Exercise--determining whether a given graph has a valid topological sequence (topological sort judgment ring)

Source: Internet
Author: User
Tags in degrees

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)

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.