本文主要是介绍hdu 5154 Harry and Magical Computer(拓扑排序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:hdu 5154 Harry and Magical Computer
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>using namespace std;
const int maxn = 105;int N, M, s[maxn], v[maxn];
vector<int> g[maxn];void init () {int a, b;memset(s, 0, sizeof(s));memset(v, 0, sizeof(v));for (int i = 0; i <= N; i++)g[i].clear();for (int i = 0; i < M; i++) {scanf("%d%d", &a, &b);g[b].push_back(a);s[a]++;}
}int find () {for (int i = 1; i <= N; i++) {if (v[i]) continue;if (s[i] == 0)return i;}return 0;
}bool judge () {for (int i = 0; i < N; i++) {int u = find();if (u == 0)return false;v[u] = 1;for (int j = 0; j < g[u].size(); j++)s[g[u][j]]--;}return true;
}int main () {while (scanf("%d%d", &N, &M) == 2) {init();printf("%s\n", judge() ? "YES" : "NO");}return 0;
}
这篇关于hdu 5154 Harry and Magical Computer(拓扑排序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!