HDU 3342 Legal or not (to determine whether the graph is topological and orderly for a given graph)

Source: Internet
Author: User

Code:
#include <cstdio> #include <cstring>using namespace std;int n,m;int mat[105][105];int in[105];int stack[105    ],top;void topo () {top=0;            for (int i=0; i<n; i++) {if (in[i]==0) {stack[++top]=i;        In[i]=-1;    }} int cnt=0;        while (top!=0) {int j=stack[top--];        cnt++;            for (int i=0; i<n; i++) {if (Mat[j][i]) {in[i]-= (mat[j][i]);                } if (in[i]==0) {stack[++top]=i;            In[i]=-1;    }}} if (cnt==n) printf ("yes\n"); else printf ("no\n");} int main () {while (scanf ("%d%d", &n,&m) && (n| |        m)) {memset (mat,0,sizeof (MAT));        memset (In,0,sizeof (in));            for (int i=0; i<m; i++) {int A, B;            scanf ("%d%d", &a,&b);            mat[a][b]++;        in[b]++;    } topo (); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3342 Legal or not (to determine whether the graph is topological and orderly for a given graph)

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.