思路:
维护入度数组,每次输出入度为0的点,若最终仍有节点则有环。
dfs的思路
若图中无环,利用dfs最后到的节点的出度为0,得到拓扑排序的逆序。
紫书上的判环思路,利用vis数组,已访问为1,正在访问为-1,未访问为0。
最后补上紫书做法
维护入度数组,每次输出入度为0的点,若最终仍有节点则有环。
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 105
#define maxm 6000005
#define mod 10007
#define INF 1000000007
#define EPS 1e-7
#define PI 3.1415926535898
#define N 4294967296
using namespace std;
//-------------------------CHC------------------------------//
vector<int> edges;
vector<int> G[maxn];
int Indegree[maxn];
void init(int n) {
for (int i = 1; i <= n; ++i) G[i].clear();
CLEAR(Indegree, 0);
edges.clear();
}
void add(int u, int v) {
edges.push_back(v);
G[u].push_back(edges.size() - 1);
}
bool vis[maxn];
void TopSort(int n) {
CLEAR(vis, 0);
queue<int> q;
for (int i = 1; i <= n; ++i)
if (Indegree[i] == 0) q.push(i);
bool first = true;
while (q.size()) {
int u = q.front(); q.pop();
vis[u] = true;
if (first) first = false;
else putchar(' ');
printf("%d", u);
for (int i = 0; i < G[u].size(); ++i) {
int v = edges[G[u][i]]; //注意
if (!vis[v] && --Indegree[v] == 0) q.push(v);
}
}
puts("");
}
void debug(int n) {
for (int i = 1; i <= n; ++i) {
printf("%d", i);
for (int j = 0; j < G[i].size(); ++j)
printf(" %d", edges[G[i][j]]);
puts("");
}
}
int main() {
//IN(); OUT();
int n, m;
while (scanf("%d%d", &n, &m) && (n || m)) { //在这出问题我很难受。
init(n);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
add(u, v);
Indegree[v]++;
}
//debug(n);
TopSort(n);
}
return 0;
}
dfs的思路
若图中无环,利用dfs最后到的节点的出度为0,得到拓扑排序的逆序。
紫书上的判环思路,利用vis数组,已访问为1,正在访问为-1,未访问为0。
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 105
#define maxm 6000005
#define mod 10007
#define INF 1000000007
#define EPS 1e-7
#define PI 3.1415926535898
#define N 4294967296
using namespace std;
//-------------------------CHC------------------------------//
vector<int> edges;
vector<int> G[maxn];
vector<int> ans;
bool vis[maxn];
void init(int n) {
for (int i = 1; i <= n; ++i) G[i].clear();
edges.clear();
ans.clear();
CLEAR(vis, 0);
}
void add(int u, int v) {
edges.push_back(v);
G[u].push_back(edges.size() - 1);
}
void dfs(int i) {
if (vis[i]) return;
vis[i] = true;
for (int j = 0; j < G[i].size(); ++j) {
int v = edges[G[i][j]];
dfs(v);
}
ans.push_back(i);
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) && (n || m)) {
init(n);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
add(u, v);
}
for(int i = 1; i <= n; ++i) dfs(i);
printf("%d", ans.back());
for (int i = ans.size() - 2; i >= 0; --i)
printf(" %d", ans[i]);
puts("");
}
return 0;
}
最后补上紫书做法
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 105
#define maxm 6000005
#define mod 10007
#define INF 1000000007
#define EPS 1e-7
#define PI 3.1415926535898
#define N 4294967296
using namespace std;
//-------------------------CHC------------------------------//
vector<int> edges;
vector<int> G[maxn];
vector<int> ans;
int vis[maxn];
void init(int n) {
for (int i = 1; i <= n; ++i) G[i].clear();
edges.clear();
ans.clear();
CLEAR(vis, 0);
}
void add(int u, int v) {
edges.push_back(v);
G[u].push_back(edges.size() - 1);
}
bool dfs(int i) {
bool ret = true;
vis[i] = -1;
for (int j = 0; j < G[i].size(); ++j) {
int v = edges[G[i][j]];
if (vis[v] == -1) ret = false;
else if (!vis[v] && !dfs(v)) ret = false;
}
vis[i] = 1;
ans.push_back(i);
return ret;
}
bool topo(int n) {
bool ret = true;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && !dfs(i)) ret = false;
}
return ret;
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) && (n || m)) {
init(n);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
add(u, v);
}
if (!topo(n)) puts("NO");
else {
printf("%d", ans.back());
for (int i = ans.size() - 2; i >= 0; --i)
printf(" %d", ans[i]);
puts("");
}
}
return 0;
}