标签:
神器气爽!就是简单的深搜和广搜啦!
#include <iostream> #include <queue> using namespace std; bool map[10][10]; bool dfsed[10]; int n, e; queue<int> Q; bool bfsed[10]; void bfs(int x); void dfs(int x); int main() { cin >> n >> e; for (int i = 0; i < e; i++){ int a, b; cin >> a >> b; map[a][b] = map[b][a] = true; } for (int i = 0; i < n; i++){ if (dfsed[i] == false){ dfsed[i] = true; cout << "{ "; dfs(i); cout << "}"; cout << endl; } } for (int i = 0; i < n; i++){ if (bfsed[i] == false){ cout <<"{ " << i << " "; Q.push(i); bfsed[i] = true; bfs(i); cout << "}" << endl; } } } void dfs(int x) { cout << x << " "; for (int i = 0; i < n; i++){ if (dfsed[i] == false && map[x][i]==true){ dfsed[i] = true; dfs(i); } } } void bfs(int x) { while (Q.empty() != true){ int k = Q.front(); Q.pop(); for (int i = 0; i < n; i++){ if (map[k][i] == true && bfsed[i] == false){ bfsed[i] = true; cout << i << " "; Q.push(i); } } } }
标签:
原文地址:http://www.cnblogs.com/zhouyiji/p/4562365.html