Hdu5154-harry and Magical computer:http://acm.hdu.edu.cn/showproblem.php?pid=5154
Or the extension of the order, directly on the code.
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; const int MAXN = 105; int Map[maxn][maxn],in[maxn],flag;int n,m;int x,y;void Initial () {flag = 0;memset (map,0,sizeof (MAP)); Memset (in,0, sizeof (in)); void Toposort () {int x;queue<int>ans;for (int i = 1;i <= n;i++) if (! In[i]) Ans.push (i),///zero-in points enter the queue while (!ans.empty ()) {x = Ans.front (); Ans.pop (), for (int i = 1;i <= n;i++) {if (Map[x][i] {//in[i]--, map[x][i] = 0;in[i]-= map[x][i],map[x][i] = 0;if (! In[i]) Ans.push (i);}}} for (int i = 1;i <= n;i++) {if (In[i])//There is also a point in the degree greater than 0 {flag = 1;break;}} if (flag) printf ("no\n"), Else printf ("yes\n");} int main () {while (~scanf ("%d%d", &n,&m)) {Initial (); while (m--) {scanf ("%d%d", &x,&y); in[x]++;// Note is the entry of x plus one map[y][x]++;//map[y][x] = 1;} Toposort ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu5154-harry and magical computer sorting