拓扑排序 HDU 3342
拓扑排序判断各工程先后顺序是否出现矛盾(a -> b, b -> c, c -> a)
BFS在这方面较DFS有些先天优势,统计入队结点数,看是否等于工程数
DFS的vis[]数组改为0, -1, 1三个状态,0代表未访问,-1代表访问完毕,1代表是这一阶段正在访问的 (注意 if(-1) 相当于 if(1) )【坑了自己一阵】
注意n个工程是(0 ~ n - 1 )还是 (1 ~ n)
Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many “holy cows” like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost “master”, and Lost will have a nice “prentice”. By and by, there are many pairs of “master and prentice”. But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not? We all know a master can have many prentices and a prentice may have a lot of masters too, it’s legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian’s master and, at the same time, 3xian is HH’s master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the “master and prentice” relation is transitive. It means that if A is B’s master ans B is C’s master, then A is C’s master.
Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y’s master and y is x’s prentice. The input is terminated by N = 0. TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,…, N-1). We use their numbers instead of their names.
Output
For each test case, print in one line the judgement of the messy relationship. If it is legal, output “YES”, otherwise “NO”.
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
Sample Output
YES
NO
Code(DFS)
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int vis[105];
stack<int> ans;
vector<int> v[105];
bool dfs(int x)
{
vis[x] = 1;
for(int i = 0; i < v[x].size(); ++i)
{
if( vis[ v[x][i] ] == 1 )///递归时搜到自己
return 0;
if( vis[ v[x][i] ] == 0 && !dfs(v[x][i]) )
return 0;
}
vis[x] = -1;
// ans.push(x);
return 1;
}
int main()
{
int n, m;
while(cin >> n >> m && (n || m))
{
bool flag = 1;
memset(vis, 0, sizeof(vis));///多组输入要加上
for(int i = 0; i < n; ++i)
v[i].clear();
for(int i = 0; i < m; ++i)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
}
for(int i = 0; i < n; ++i)///不相连的多个图
if(vis[i] == 0)
{
if(!dfs(i))
{
flag = 0;
break;
}
}
// while(ans.size())
// {
// printf("%d%c", ans.top(), ans.size() == 1 ? '\n' : ' ');
// ans.pop();
// }
if(flag)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
return 0;
}
Code(BFS)
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
int in[105];
//vector<int> ans;
vector<int> v[105];
bool bfs(queue<int> q)
{
int sum = 0;
while(q.size())
{
int tem = q.front();
// ans.push_back(tem);
sum++;
q.pop();
for(int i = 0; i < v[tem].size(); ++i)
{
in[ v[tem][i] ]--;
if(!in[ v[tem][i] ])
q.push(v[tem][i]);
}
}
return sum == n;
}
int main()
{
while(cin >> n >> m && (n || m))
{
queue<int> q;
memset(in, 0, sizeof(in));
// ans.clear();
for(int i = 0; i < n; ++i)
v[i].clear();
int a, b;
for(int i = 0; i < m; ++i)
{
cin >> a >> b;
v[a].push_back(b);
in[b]++;
}
for(int i = 0; i < n; ++i)///0 ~ n - 1
if(!in[i])
q.push(i);
cout << ( bfs(q) ? "YES" : "NO" ) << '\n';
}
return 0;
}