PAT (Advanced Level) Practice 圖的遍歷 相關題ios
vector<int> Adj[N]; Adj[u].push_back(v);
struct Node{ int v; int weight; }; vector<Node> Adj[N]; Adj[u].push_back({v,weight});
若已知給定圖爲連通圖,則只需一次 DFS 便可完成遍歷算法
const int MAXV = 1000; const INF = 1000000000;
int n, G[MAXV][MAXV]; bool vis[MAXV] = {false}; void DFS(int u, int depth){ vis[u] = true; // 若須要對 u 進行一些操做,在這裏進行 for (int v = 0; v < n; v++) if (!vis[v] && G[u][v] != INF) DFS(v, depth + 1); } void DFSTrave(){ for (int u = 0; u < n; u++) if (!vis[u]) DFS(u, 1); }
vector<int> Adj[MAXV]; int n; bool vis[MAXV] = {false}; void DFS(int u, int depth){ vis[u] = true; // 若須要對 u 進行一些操做,在這裏進行 for(int i = 0; i < Adj[u].size(); i++){ int v = Adj[u][i]; if (!vis[v]) DFS(v, depth + 1); } } void DFSTrave(){ for (int u = 0; u < n; u++) if (!vis[u]) DFS(u,1); }
int n, G[MAXV][MAXV]; bool inq[MAXV] = {false}; void BFS(int u){ queue<int> q; q.push(u); inq[u] = true; while (!q.empty()){ int u = q.front(); q.pop(); for (int v = 0; v < n; v++){ if (!inq[v] && G[u][v] != INF){ q.push(v); inq[v] = true; } } } } void BFSTrave(){ for (int u = 0; u < n; u++) if (!inq[u]) BFS(u); }
vector<int> Adj[MAXN]; int n; bool inq[MAXN] = {false}; void BFS(int u){ queue<int> q; q.push(u); inq[u] = true; while (!q.empty()){ int u = q.front(); q.pop(); for (int i = 0; i < Adj[u].size(); i++){ int v = Adj[u][i]; if (!inq[v]){ q.push(v); inq[v] = true; } } } } void BFSTrave(){ for (int u = 0; u < n; u++) if (!inq[u]) BFS(u); }
struct Node{ int v; int level; }; vector<Node> Adj[N]; void BFS(int s){ queue<Node> q; Node start = {s,0}; q.push(start); inq[start.v] = true; while(!q.empty()){ Node now = q.front(); q.pop(); int u = now.v; for (int i = 0; i < Adj[u].size(); i++){ Node next = Adj[u][i]; next.level = now.level + 1; if (!inq[next.v]){ q.push(next); inq[next.v] = true; } } } }
#include<iostream> #include<vector> #include<set> using namespace std; set<int> roots; vector<int> Adj[10001]; int n, components = 0, depth = 0, maxdepth = 0; bool vis[10001] = {false}; void DFS(int root, int level){ vis[root] = true; if (level > depth) depth = level; for (int i = 0; i < Adj[root].size(); i++) if (!vis[Adj[root][i]]) DFS(Adj[root][i], level+1); } void DFSTrave(){ for (int i = 1; i < n + 1; i++){ if (!vis[i]){ DFS(i, 0); components++; } } if (components == 1){ for (int i = 1; i < n + 1; i++){ fill(vis, vis+10001, false); depth = 0; DFS(i,0); if (depth == maxdepth) roots.insert(i); else if (depth > maxdepth){ maxdepth = depth; roots.clear(); roots.insert(i); } } } } int main() { int u, v; scanf("%d", &n); for (int i = 1; i < n; i++){ scanf("%d%d", &u, &v); Adj[u].push_back(v); Adj[v].push_back(u); } DFSTrave(); if (components > 1) printf("Error: %d components\n", components); else for (auto it: roots) printf("%d\n",it); return 0; }
#include<iostream> #include<vector> #include<queue> using namespace std; struct Node{ int id, level; }; int maxlevel, numforward; vector<Node> Adj[1001]; void BFS(int start){ bool inq[1001] = {false}; queue<Node> q; q.push({start,0}); inq[start] = true; while (!q.empty()){ Node now = q.front(); q.pop(); for (int i = 0; i < Adj[now.id].size(); i++){ Node next = Adj[now.id][i]; next.level = now.level + 1; if (!inq[next.id] && next.level <= maxlevel){ inq[next.id] = true; q.push(next); numforward++; } } } } int main() { int n, m, followed, k, query; scanf("%d%d", &n, &maxlevel); for (int i = 1; i < n + 1; i++){ scanf("%d", &m); for (int j = 0; j < m; j++){ scanf("%d", &followed); Adj[followed].push_back({i,0}); } } scanf("%d", &k); for (int i = 0; i < k; i++){ scanf("%d", &query); numforward = 0; BFS(query); printf("%d\n", numforward); } return 0; }